SpringBoot學習第一篇:構建第一個SpringBoot工程
https://www.fangzhipeng.com/springboot/2017/07/11/springboot1
本文出自方誌朋的部落格
簡介
spring boot 它的設計目的就是為例簡化開發,開啟了各種自動裝配,你不想寫各種配置檔案,引入相關的依賴就能迅速搭建起一個web工程。它採用的是建立生產就緒的應用程式觀點,優先於配置的慣例。
可能你有很多理由不放棄SSM,SSH,但是當你一旦使用了springboot ,你會覺得一切變得簡單了,配置變的簡單了、編碼變的簡單了,部署變的簡單了,感覺自己健步如飛,開發速度大大提高了。就好比,當你用了IDEA,你會覺得再也回不到Eclipse時代一樣。另,本系列教程全部用的IDEA作為開發工具。
建構工程
你需要:
15分鐘
jdk 1.8或以上
maven 3.0+
Idea
開啟Idea-> new Project ->Spring Initializr ->填寫group、artifact ->鉤上web(開啟web功能)->點下一步就行了。
工程目錄
建立完工程,工程的目錄結構如下:
- src
-main
-java
-package
-SpringbootApplication
-resouces
- statics
- templates
- application.yml
-test
- pom
pom檔案為基本的依賴管理檔案
resouces 資原始檔
statics 靜態資源
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>
其中spring-boot-starter-web不僅包含spring-boot-starter,還自動開啟了web功能。
功能演示
說了這麼多,你可能還體會不到,舉個栗子,比如你引入了Thymeleaf的依賴,spring boot 就會自動幫你引入SpringTemplateEngine,當你引入了自己的SpringTemplateEngine,spring boot就不會幫你引入。它讓你專注於你的自己的業務開發,而不是各種配置。
再舉個栗子,建個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!";
}
}
啟動SpringbootFirstApplication的main方法,開啟瀏覽器localhost:8080,瀏覽器顯示:
Greetings from Spring Boot!
神奇之處:
你沒有做任何的web.xml配置。
你沒有做任何的sping mvc的配置; springboot為你做了。
你沒有配置tomcat ;springboot內嵌tomcat.
啟動springboot 方式
cd到專案主目錄:
mvn clean
mvn package 編譯專案的jar
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);
}
@Bean
public 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);
}
};
}
}
程式輸出:
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 {
@LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate template;
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(),
String.class);
assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
}
}
執行它會先開啟sprigboot工程,然後再測試,測試通過 .
原始碼下載:https://github.com/forezp/SpringBootLearning
結語
市面上有很多springboot的書,有很多springboot的部落格,為什麼我還要寫這樣一個系列?到目前為止,我沒有看過一本springboot的書,因為還沒來得及看,看的都是官方指南,當然也參考了很多的部落格,他們都寫的非常的棒!在看官方指南和部落格的時候,發現他們有很多不同之處,所以我打算寫一個來源於官方,通過自己理解加整合寫一個系列,所以取名叫《springboot 非官方教程》。我相信我寫的可能跟其他人的寫的會不太一樣。另外,最主要的原因還是提高自己,懷著一個樂於分享的心,將自己的理解分享給更多需要的人。