SpringBoot的starter到底是什么?( 四 )

我們可以看到 , 這個文件比較簡單就引入了:

  • spring-boot-starter:springboot的相關jar包 。
  • spring-boot-autoconfigure:springboot自動配置相關jar包 。
  • spring-boot-configuration-processor:springboot生成IDE提示功能相關jar包 。
重點看看spring.factories文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sue.IdGenerateAutoConfiguration它里面只包含一行配置,其中key是EnableAutoConfiguration,value是IdGenerateAutoConfiguration 。
再重點看一下IdGenerateAutoConfiguration
@ConditionalOnClass(IdProperties.class)@EnableConfigurationProperties(IdProperties.class)@Configurationpublic class IdGenerateAutoConfiguration {@Autowiredprivate IdProperties properties;@Beanpublic IdGenerateService idGenerateService() {return new IdGenerateService(properties.getWorkId());}}該類是一個使用了@Configuration注解標記為了配置類 , 生效的條件是@ConditionalOnClass注解中檢測到包含IdProperties.class 。并且使用@EnableConfigurationProperties注解會自動注入IdProperties的實例 。
此外,最關鍵的點是該類里面創建了idGenerateService的bean實例,這是自動配置的精髓 。
再看看IdGenerateService
public class IdGenerateService {private Long workId;public IdGenerateService(Long workId) {this.workId = workId;}public Long generate() {return new Random().nextInt(100) + this.workId;}}我們可以看到它是一個普通的類,甚至都沒有使用@Service注解,里面有個generate方法,根據workId的值和隨機數動態生成id 。
最后看看IdProperties
@ConfigurationProperties(prefix = IdProperties.PREFIX)public class IdProperties {public static final String PREFIX = "sue";private Long workId;public Long getWorkId() {return workId;}public void setWorkId(Long workId) {this.workId = workId;}}它是一個配置實體類 , 里面包含了相關的配置文件 。使用@ConfigurationProperties注解,會自動把application.properties文件中以sue開通的,參數名稱跟IdProperties中一樣的參數值,自動注入到IdProperties對象中 。
3.3 創建id-generate-test這個項目主要用于測試 。
SpringBoot的starter到底是什么?

文章插圖
該項目里面包含:pom.xml、application.properties、Application 和 TestRunner 文件 。
先看看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><version>1.3.1</version><groupId>com.sue</groupId><artifactId>spring-boot-id-generate-test</artifactId><name>spring-boot-id-generate-test</name><dependencies><dependency><groupId>com.sue</groupId><artifactId>id-generate-spring-boot-starter</artifactId><version>1.3.1</version></dependency></dependencies></project>由于只測試剛剛定義的id生成功能,所以只引入的id-generate-spring-boot-starter jar包 。
application.properties配置資源文件
sue.workId=123只有一行配置,因為我們的IdProperties中目前只需要這一個參數 。
Application是測試程序啟動類
@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}很簡單,就是一個普通的springboot啟動類
TestRunner是我們的測試類
@Componentpublic class TestRunner implements ApplicationRunner {@Autowiredprivate IdGenerateService idGenerateService;public void run(ApplicationArguments args) throws Exception {Long sysNo = idGenerateService.generate();System.out.println(sysNo);}}它實現了ApplicationRunner接口,所以在springboot啟動的時候會調用該類的run方法 。
好了 , 所有自定義starter的代碼和測試代碼都已經就緒 。接下,運行一下Application類的main方法 。
運行結果:
176完美,驗證成功了 。
接下來,我們分析一下starter的底層實現原理 。
4 starter的底層原理是什么?通過上面編寫自己的starter的例子,相信大家對starter的認識更進一步了,現在跟大家一起看看starter的底層是如何實現的 。
id-generate-starter.jar其實是一個空項目,依賴于id-generate-autoconfiguration.jar 。
id-generate-starter.jar是一個入口,我們給他取一個更優雅的名字:

推薦閱讀