springboot-custom starter
Spring Boot由眾多Starter組成,隨著版本的推移Starter家族成員也與日俱增。在傳統Maven項目中通常將一些層、組件拆分為模塊來管理, 以便相互依賴復用,在Spring Boot項目中我們則可以創建自定義Spring Boot Starter來達成該目的。
可以認為starter是一種服務——使得使用某個功能的開發者不需要關注各種依賴庫的處理,不需要具體的配置信息, 由Spring Boot自動通過classpath路徑下的類發現需要的Bean,并織入相應的Bean。舉個栗子,spring-boot-starter-jdbc這個starter的存在, 使得我們只需要在BookPubApplication下用@Autowired引入DataSource的bean就可以,Spring Boot會自動創建DataSource的實例。
本篇將通過一個簡單的例子來演示如何編寫自己的starter。
這里講一下我們的Starter要實現的功能,很簡單,提供一個Service,包含一個能夠將字符串加上前后綴的方法String wrap(String word)。 而具體的前綴和后綴是通過讀取SpringBoot配置文件application.yml而獲取的。
?
添加maven依賴
第一步當然是創建一個maven工程,添加SpringBoot的自動配置的依賴:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.3.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.code</groupId> 12 <artifactId>simple-spring-boot-starter</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>simple-spring-boot-starter</name> 15 <description>springboot-starter</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter</artifactId> 25 </dependency> 26 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-test</artifactId> 30 <scope>test</scope> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-configuration-processor</artifactId> 36 <optional>true</optional> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-autoconfigure</artifactId> 41 </dependency> 42 </dependencies> 43 44 <build> 45 <plugins> 46 <plugin> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-maven-plugin</artifactId> 49 <configuration> 50 <skip>true</skip> 51 </configuration> 52 </plugin> 53 </plugins> 54 </build> 55 56 </project>注意其中?spring-boot-configuration-processor?的作用是編譯時生成spring-configuration-metadata.json, 此文件主要給IDE使用,用于提示使用。如在intellij idea中,當配置此jar相關配置屬性在application.yml, 你可以用ctlr+鼠標左鍵,IDE會跳轉到你配置此屬性的類中。
這里說下artifactId的命名問題,Spring 官方 Starter通常命名為spring-boot-starter-{name}?如?spring-boot-starter-web。
Spring官方建議非官方Starter命名應遵循{name}-spring-boot-starter的格式。
?
編寫Service?
1 public class ExampleService { 2 3 private String prefix; 4 private String suffix; 5 6 public ExampleService(String prefix, String suffix) { 7 this.prefix = prefix; 8 this.suffix = suffix; 9 } 10 public String wrap(String word) { 11 return prefix + word + suffix; 12 } 13 }?
編寫屬性類
?
1 @ConfigurationProperties("example.service") 2 public class ExampleServiceProperties { 3 private String prefix; 4 private String suffix; 5 6 public String getPrefix() { 7 return prefix; 8 } 9 10 public void setPrefix(String prefix) { 11 this.prefix = prefix; 12 } 13 14 public String getSuffix() { 15 return suffix; 16 } 17 18 public void setSuffix(String suffix) { 19 this.suffix = suffix; 20 } 21 }?
編寫自動配置類
?
1 @Configuration 2 @ConditionalOnClass(ExampleService.class) 3 @EnableConfigurationProperties(ExampleServiceProperties.class) 4 public class ExampleAutoConfigure { 5 6 private final ExampleServiceProperties properties; 7 8 @Autowired 9 public ExampleAutoConfigure(ExampleServiceProperties properties) { 10 this.properties = properties; 11 } 12 13 @Bean 14 @ConditionalOnMissingBean 15 @ConditionalOnProperty(prefix = "example.service", value = "enabled",havingValue = "true") 16 ExampleService exampleService (){ 17 return new ExampleService(properties.getPrefix(),properties.getSuffix()); 18 } 19 20 }?
解釋下用到的幾個和Starter相關的注解:
1. @ConditionalOnClass,當classpath下發現該類的情況下進行自動配置。 2. @ConditionalOnMissingBean,當Spring Context中不存在該Bean時。 3. @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true"),當配置文件中example.service.enabled=true時。?
添加spring.factories
最后一步,在resources/META-INF/下創建spring.factories文件,內容供參考下面:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.code.starter.config.ExampleAutoConfigure如果有多個自動配置類,用逗號分隔換行即可。
OK,完事,運行?mvn:install?打包安裝,一個Spring Boot Starter便開發完成了。
測試
打包好了當然要測試一下看看了。另外創建一個SpringBoot工程,在maven中引入這個starter依賴, 然后在單元測試中引入這個Service看看效果。
1 <dependency> 2 <groupId>com.code</groupId> 3 <artifactId>simple-spring-boot-starter</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-test</artifactId> 9 <scope>test</scope> 10 </dependency>修改application.yml配置文件,添加如下內容:
example.service:enabled: trueprefix: prefixsuffix: suffix測試類:
1 @RunWith(SpringRunner.class) 2 @SpringBootTest 3 public class ApplicationTests { 4 @Autowired 5 private ExampleService exampleService; 6 7 @Test 8 public void testStarter() { 9 System.out.println(exampleService.wrap("hello")); 10 } 11 }運行結果:
prefixhellosuffix?
總結
總結下Starter的工作原理:
轉載于:https://www.cnblogs.com/UniqueColor/p/10571915.html
總結
以上是生活随笔為你收集整理的springboot-custom starter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git/github使用详解
- 下一篇: #012python实验课