SpringBoot的介紹
簡(jiǎn)單來(lái)說(shuō),SpringBoot就是Spring提供的用于Web開(kāi)發(fā)的腳手架框架。配置簡(jiǎn)單、上手快速
SpringBoot的特性
- 自帶tomcat、Jetty服務(wù)器可以部署war包
- 自動(dòng)配置Spring框架和第三方框架
- 能夠提供應(yīng)用的健康監(jiān)控和配置的監(jiān)控
- 沒(méi)有代碼生成,并且盡可能的減少了xml等的配置
SpringBoot的創(chuàng)建
在線創(chuàng)建
通過(guò) start.spring.io 來(lái)創(chuàng)建

通過(guò)idea創(chuàng)建
創(chuàng)建maven工程項(xiàng)目
添加maven依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
添加啟動(dòng)類
@EnableAutoConfiguration
@RestController
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
SpringBoot的相關(guān)知識(shí)點(diǎn)分析
@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM,
classes = AutoConfigurationExcludeFilter.class) })
@Configuration
SpringBoot加載時(shí)會(huì)加載很多的后置處理器,各種后置處理器完成各種處理功能。其中有一個(gè)后置處理器ConfigurationClassPostProcessor。這個(gè)后置處理器就是專門負(fù)責(zé)處理帶@Configuration的類。所以在SpringBoot加載的時(shí)候加載到所有帶有@Configuration標(biāo)記的類,在類中使用了一個(gè)代理類來(lái)增強(qiáng)了被@Configuration標(biāo)記的類。原來(lái)的類會(huì)被增強(qiáng)的類替換。被代理的類執(zhí)行的時(shí)候會(huì)執(zhí)行一個(gè)BeanMethodInterceptor攔截器,在攔截器中會(huì)攔截加了@Bean的方法,然后會(huì)判斷@Bean方法的Bean是否被創(chuàng)建。如果創(chuàng)建了就會(huì)直接返回。沒(méi)有Bean就會(huì)創(chuàng)建。所以@Configuration標(biāo)記的類創(chuàng)建的對(duì)象是同一個(gè)對(duì)象。
如果沒(méi)有使用@Configuration,使用的是@Component,那么這個(gè)類就沒(méi)有被增強(qiáng)沒(méi)有被代理,那這個(gè)類就是普通的類,就會(huì)和普通類一樣創(chuàng)建對(duì)象,創(chuàng)建的就不一定是同一個(gè)對(duì)象
SpringBoot的Parent
SpringBoot的Parent完成了以下的功能,使得我們的項(xiàng)目繼承SpringBootParent后可以直接省略這些步驟
- 定義Java的編譯版本
- 定義項(xiàng)目編碼格式
- 定義依賴的版本號(hào)
- 項(xiàng)目打包的配置
- 資源文件的過(guò)濾
SpringBootParent使用的兩種方式
- 第一種 直接繼承SpringBootParent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
- 第二種在項(xiàng)目中使用SpringBootDependencies
當(dāng)我們的項(xiàng)目不方便繼承SpringBootParent或者我們需要繼承自己的Parent的時(shí)候,我們可以使用SpringBootDependencies
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>