javascript
Spring Boot(二):Spring Boot中的Starter介绍
一、簡介
添加Starter使用步驟:
二、Starter原理介紹
在使用SpringBoot做Web開發時,我們會在pom中加入spring-boot-starter-web的依賴。
Starter的jar中沒有一句java代碼,有一個pom文件。
引入啟動器依賴的作用有兩個:
如spring-boot-starter-web會引入spring-webmvc等模塊的依賴,引入自己所需要的依賴。
starter都會引入spring-boot-starter的依賴,spring-boot-starter會引入spring-boot-autoconfigure的依賴。(任意一個starter都會引入spring組件的所有的自動配置,如我們引入we的starter也會將Spring的Cloud的自動配置加載進入容器)[括號中是我自己的理解,如果知道小伙伴可以評論交流一下]而1中的依賴確實是各個場景所需要的才會引入。
如何將自動配置類加載進容器的:
我們引入starter的依賴,會將自動配置的類的jar引入。@SpringBootApplication的注解中有一個是@EnableAutoConfiguration注解,這個注解在定義時(對這個不理解的小伙伴可以看一下之前的文章中的第三部分入門程序探究的注解部分),有一個@Import({EnableAutoConfigurationImportSelector.class}),EnableAutoConfigurationImportSelector內部則是使用了SpringFactoriesLoader.loadFactoryNames方法進行掃描具有META-INF/spring.factories文件的jar包。而自動配置類的jar的是有一個META-INF/spring.factories文件內容如下:
\是為了換行也可以使用,可以看到配置的結構形式是Key=>Value形式,多個Value時使用,隔開。
應用在啟動時就會加載spring-boot-autoconfigure的jar包下面的META-INF/spring.factories文件中定義的autoconfiguration類。將configuration類中定義的bean加入spring到容器中。就相當于加載之前我們自己配置組件的xml文件。而現在SpringBoot自己定義了一個默認的值,然后直接加載進入了Spring容器。
這一部分更多的細節可以看看這兩篇文章:
https://segmentfault.com/a/1190000011433487#articleHeader5
https://www.jianshu.com/p/29f3a2f65282
那怎么更改默認的配置:
SpringBoot之所以流行的原因,主要就是因為自動配置依賴默認支持和提供了很多自動配置依賴模塊。所有的spring-boot-starter都有約定俗成的默認配置,但允許調整這些配置調整默認的行為。
雖然不同的starter實現起來各有差異,但是他們基本上都會使用到兩個相同的內容:ConfigurationProperties和AutoConfiguration。因為Spring Boot堅信“約定大于配置”這一理念,所以我們使用ConfigurationProperties來保存我們的配置,并且這些配置都可以有一個默認值,即在我們沒有主動覆寫原始配置的情況下,默認值就會生效,這在很多情況下是非常有用的。除此之外,starter的ConfigurationProperties還使得所有的配置屬性被聚集到一個文件中(properties文件),這樣我們就告別了Spring項目中XML地獄。
我們來看一看starter的整體邏輯
所以只要我們在application.properties中了寫入配置信息就會覆蓋掉默認的配置。
這一部分更多的細節可以查看
https://www.nosuchfield.com/2017/10/15/Spring-Boot-Starters/
我們可以通過創建自己的SpringBootStarter來了解更清楚:
我們會創建兩個模塊:
1.啟動器只用來做依賴導入;
2.自動配置模塊,專門來寫一個自動配置模塊;
(1)啟動器模塊
啟動器只是用來引入自動配置模塊和場景所需要的依賴。本場景中沒有引入其他的依賴。只引入了自動配置模塊。啟動啟動模塊部分到此結束。
啟動啟動模塊只需要一個pom文件即可。
(2)自動配置模塊
自動配置模塊的pom文件
<?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.atguigu.starter</groupId><artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>atguigu-spring-boot-starter-autoconfigurer</name><description>Demo project for Spring Boot</description><!--所有starter的父文件--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.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><!--引入spring-boot-starter;所有starter的基本配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies> </project>proproperties類,用來保存默認的配置。
package com.atguigu.starter;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "atguigu.hello") public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;} }使用配置的類。
package com.atguigu.starter;public class HelloService {HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}public String sayHellAtguigu(String name){return helloProperties.getPrefix()+"-" +name + helloProperties.getSuffix();} }創建一個AutoConfiguration,引用定義好的配置信息;在AutoConfiguration中實現所有starter應該完成的操作,并且把這個類加入spring.factories配置文件中進行聲明。
package com.atguigu.starter;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration @ConditionalOnWebApplication //web應用才生效//將帶有@ConfigurationProperties注解的類注入為Spring容器的Bean @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration {@AutowiredHelloProperties helloProperties;@Beanpublic HelloService helloService(){HelloService service = new HelloService();service.setHelloProperties(helloProperties);return service;} }打包項目安裝到Maven倉庫,之后在一個SpringBoot項目中引入該項目依賴,然后就可以使用該starter了。
更詳細的可以看這篇博客
https://www.nosuchfield.com/2017/10/15/Spring-Boot-Starters/
總結
以上是生活随笔為你收集整理的Spring Boot(二):Spring Boot中的Starter介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL 子查询
- 下一篇: gradle idea java ssm