SpringBoot定制化Starter實現(xiàn)方法
Spring Boot Starter官網(wǎng)描述:Spring Boot Starter官方介紹
什么是Spring Boot Starter
Starters可以理解為啟動器,它包含了一系列可以集成到應用里面的依賴包,可以一站式集成 Spring和其他技術,而不需要到處找示例代碼和依賴包。Spring Boot Starter的工作原理是:Spring Boot在啟動時掃描項目所依賴的JAR包,尋找包含spring.factories文件的JAR包,根據(jù)spring.factories
配置加載AutoConfigure
類,根據(jù)@Conditional
注解的條件,進行自動配置并將Bean注入Spring Context
為什么要自定義Spring Boot Starter?
在Spring Boot官網(wǎng)為了簡化我們的開發(fā),已經(jīng)提供了非常多場景的Starter來為我們使用,即便如此,也無法全面的滿足我們實際工作中的開發(fā)場景,這時我們就需要自定義實現(xiàn)定制化的Starter。
實現(xiàn)步驟
1.首先,創(chuàng)建一個Maven空工程,添加兩個模塊
啟動器
啟動器中沒有任何的源代碼,只是告訴我們當前場景需要引入哪些依賴即可!
創(chuàng)建啟動器模塊為maven工程,命名為
xiaozhao-hello-spring-boot-starter
,對應的依賴文件
<groupId>com.zhao</groupId> <artifactId>xiaozhao-hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version>
自動配置包
自動配置包中實現(xiàn)了所有的自動配置功能!
創(chuàng)建自動配置包模塊為SpringBoot初始化工程,命名為xiaozhao-hello-spring-boot-starter-autoconfigure
最終的項目模塊如下:
2.模塊創(chuàng)建完成后,需要在啟動器中引入自動配置模塊(別人引入場景啟動器,自動配置包就會自動引入)
<dependencies> <dependency> <groupId>com.zhao</groupId> <artifactId>xiaozhao-hello-spring-boot-starter-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
3.編寫自動配置模塊。
創(chuàng)建自定義的Properties文件
@ConfigurationProperties("xiaozhao.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; } }
創(chuàng)建業(yè)務類讀取Properties文件中的值
public class HelloService { @Autowired HelloProperties helloProperties; public String sayHello(String userName){ return helloProperties.getPrefix() + ":" + userName + ">" + helloProperties.getSuffix(); } }
再個自動配置類,自動進行類加載
@Configuration @ConditionalOnMissingBean(HelloService.class) @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @Bean public HelloService helloService(){ HelloService helloService = new HelloService(); return helloService; } }
最終的效果如下:
4.在resources
目錄下創(chuàng)建META-INF/spring.factories添加如下配置信息
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhao.hello.auto.HelloServiceAutoConfiguration
5.裝hello-spring-boot-starter-autoconfigure
模塊和xiaozhao-hello-spring-boot-starter
6.裝完成后,創(chuàng)建新的項目來引入創(chuàng)建好的starter.新的的項目名為hello-test
的Spring Boot初始化項目。
引入我們之前定義的Starter啟動器和Spring Boot自帶的Web啟動器
<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> <dependency> <groupId>com.zhao</groupId> <artifactId>xiaozhao-hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
7.在hello-test
項目中創(chuàng)建測試Controller
@RestController public class HelloController { @Autowired HelloService helloService; @GetMapping("/hello") public String sayHello() { String str = helloService.sayHello("李四"); return str; } }
編寫配置文件
xiaozhao.hello.prefix=hello
xiaozhao.hello.suffix=666
8.啟動項目,測試一下
總結自定義Starter的實現(xiàn)邏輯
- 首先引入自定義的Starter:xiaozhao-hello-spring-boot-starter,在這個Starter中引入自定義的自動配置場景
- 當自動配置場景啟動時,會去尋找spring.factories文件,去自動加載HelloServiceAutoConfiguration類文件
- 加載完自動配置類后,@ConditionalOnMissingBean(HelloService.class)通過這個注解,當容器中沒有HelloService時,去自動添加一個HelloService組件。
- HelloService組件的所有屬性,通過HelloProperties配置文件進行綁定的,@ConfigurationProperties("xiaozhao.hello"),通過xiaozhao.hello。xxx進行綁定。
- 如果自己在容器中注入一個HelloService組件,使用的就不是自動配置的,而是重新注入的。
到此這篇關于SpringBoot定制化Starter實現(xiàn)方法的文章就介紹到這了,更多相關SpringBoot Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ScrollView中嵌入ListView只顯示一條的解決辦法
在ScrollView添加一個ListView會導致listview控件顯示不全,通常只會顯示一條,究竟是什么原因呢?下面腳本之家小編給大家介紹ScrollView中嵌入ListView只顯示一條的解決辦法,感興趣的朋友一起學習吧2016-05-05解決Springboot中@Async注解獲取不到上下文信息問題
實際開發(fā)中我們經(jīng)常需要通過spring上下文獲取一些配置信息,本文主要介紹了解決Springboot中@Async注解獲取不到上下文信息問題,具有一定的參考價值,感興趣的可以了解一下2024-01-01解決Java中的java.io.IOException: Broken pipe問題
這篇文章主要介紹了解決Java中 java.io.IOException: Broken pipe的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06maven插件maven-assembly-plugin打包歸納文件zip/tar使用
java項目運行的文件需要jar或者war格式,同時還需要使用Java命令,本文主要介紹了maven插件maven-assembly-plugin打包歸納文件zip/tar使用,具有一定的參考價值,感興趣的可以了解一下2024-02-02