javascript
SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
轉(zhuǎn)載請標(biāo)明出處:?
http://blog.csdn.net/forezp/article/details/70341651?
本文出自方志朋的博客
簡介
spring boot 它的設(shè)計目的就是為例簡化開發(fā),開啟了各種自動裝配,你不想寫各種配置文件,引入相關(guān)的依賴就能迅速搭建起一個web工程。它采用的是建立生產(chǎn)就緒的應(yīng)用程序觀點,優(yōu)先于配置的慣例。
可能你有很多理由不放棄SSM,SSH,但是當(dāng)你一旦使用了springboot ,你會覺得一切變得簡單了,配置變的簡單了、編碼變的簡單了,部署變的簡單了,感覺自己健步如飛,開發(fā)速度大大提高了。就好比,當(dāng)你用了IDEA,你會覺得再也回不到Eclipse時代一樣。另,本系列教程全部用的IDEA作為開發(fā)工具。
建構(gòu)工程
你需要:
- 15分鐘
- jdk 1.8或以上
- maven 3.0+
- Idea
打開Idea-> new Project ->Spring Initializr ->填寫group、artifact ->鉤上web(開啟web功能)->點下一步就行了。
工程目錄
創(chuàng)建完工程,工程的目錄結(jié)構(gòu)如下:
- src-main-java-package-SpringbootApplication-resouces- statics- templates- application.yml-test - pom- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- pom文件為基本的依賴管理文件
- resouces 資源文件?
- statics 靜態(tài)資源
- templates 模板資源
- application.yml 配置文件
- SpringbootApplication程序的入口。
pom.xml的依賴:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forezp</groupId><artifactId>springboot-first-application</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-first-application</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
其中spring-boot-starter-web不僅包含spring-boot-starter,還自動開啟了web功能。
功能演示
說了這么多,你可能還體會不到,舉個栗子,比如你引入了Thymeleaf的依賴,spring boot 就會自動幫你引入SpringTemplateEngine,當(dāng)你引入了自己的SpringTemplateEngine,spring boot就不會幫你引入。它讓你專注于你的自己的業(yè)務(wù)開發(fā),而不是各種配置。
再舉個栗子,建個controller:
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping;@RestController public class HelloController {@RequestMapping("/")public String index() {return "Greetings from Spring Boot!";}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
啟動SpringbootFirstApplication的main方法,打開瀏覽器localhost:8080,瀏覽器顯示:
Greetings from Spring Boot!
神奇之處:
- 你沒有做任何的web.xml配置。
- 你沒有做任何的sping mvc的配置; springboot為你做了。
- 你沒有配置tomcat ;springboot內(nèi)嵌tomcat.
啟動springboot 方式
cd到項目主目錄:
mvn clean mvn package 編譯項目的jar- 1
- 2
- mvn spring-boot: run 啟動
- cd 到target目錄,java -jar 項目.jar
來看看springboot在啟動的時候為我們注入了哪些bean
在程序入口加入:
@SpringBootApplication public class SpringbootFirstApplication {public static void main(String[] args) {SpringApplication.run(SpringbootFirstApplication.class, args);}@Beanpublic CommandLineRunner commandLineRunner(ApplicationContext ctx) {return args -> {System.out.println("Let's inspect the beans provided by Spring Boot:");String[] beanNames = ctx.getBeanDefinitionNames();Arrays.sort(beanNames);for (String beanName : beanNames) {System.out.println(beanName);}};}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
程序輸出:
Let’s inspect the beans provided by Spring Boot:?
basicErrorController?
beanNameHandlerMapping?
beanNameViewResolver?
characterEncodingFilter?
commandLineRunner?
conventionErrorViewResolver?
defaultServletHandlerMapping?
defaultViewResolver?
dispatcherServlet?
dispatcherServletRegistration?
duplicateServerPropertiesDetector?
embeddedServletContainerCustomizerBeanPostProcessor?
error?
errorAttributes?
errorPageCustomizer?
errorPageRegistrarBeanPostProcessor
….?
….
在程序啟動的時候,springboot自動諸如注入了40-50個bean.
單元測試
通過@RunWith() @SpringBootTest開啟注解:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class HelloControllerIT {@LocalServerPortprivate int port;private URL base;@Autowiredprivate TestRestTemplate template;@Beforepublic void setUp() throws Exception {this.base = new URL("http://localhost:" + port + "/");}@Testpublic void getHello() throws Exception {ResponseEntity<String> response = template.getForEntity(base.toString(),String.class);assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
運行它會先開啟sprigboot工程,然后再測試,測試通過 ^.^
源碼下載:https://github.com/forezp/SpringBootLearning
結(jié)語
市面上有很多springboot的書,有很多springboot的博客,為什么我還要寫這樣一個系列?到目前為止,我沒有看過一本springboot的書,因為還沒來得及看,看的都是官方指南,當(dāng)然也參考了很多的博客,他們都寫的非常的棒!在看官方指南和博客的時候,發(fā)現(xiàn)他們有很多不同之處,所以我打算寫一個來源于官方,通過自己理解加整合寫一個系列,所以取名叫《springboot 非官方教程》。我相信我寫的可能跟其他人的寫的會不太一樣。另外,最主要的原因還是提高自己,懷著一個樂于分享的心,將自己的理解分享給更多需要的人。
參考資料
Building an Application with Spring Boot
優(yōu)秀文章推薦:
- 更多springboot 教程:springBoot非官方教程 | 終章:文章匯總
- 更多springcoud 教程:史上最簡單的 SpringCloud 教程 | 終章: 文章匯總
總結(jié)
以上是生活随笔為你收集整理的SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dva-知识地图
- 下一篇: SpringBoot非官方教程 | 第二