眾所周知(不知道?點此)栽燕,Spring Boot由眾多Starter組成罕袋,隨著版本的推移Starter家族成員也與日俱增。在傳統(tǒng)Maven項目中通常將一些層碍岔、組件拆分為模塊來管理浴讯,以便相互依賴復用,在Spring Boot項目中我們則可以創(chuàng)建自定義Spring Boot Starter來達成該目的蔼啦。
好榆纽,開始,先創(chuàng)建一個Maven項目并引入依賴询吴,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>
<groupId>com.example</groupId>
<artifactId>example-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
這里說下artifactId
的命名問題,Spring 官方 Starter通常命名為spring-boot-starter-{name}
如 spring-boot-starter-web
猛计, Spring官方建議非官方Starter命名應遵循{name}-spring-boot-starter
的格式唠摹。
這里講一下我們的Starter要實現(xiàn)的功能,很簡單奉瘤,提供一個Service
勾拉,包含一個能夠將字符串加上前后綴的方法String wrap(String word)
。
public class ExampleService {
private String prefix;
private String suffix;
public ExampleService(String prefix, String suffix) {
this.prefix = prefix;
this.suffix = suffix;
}
public String wrap(String word) {
return prefix + word + suffix;
}
}
前綴盗温、后綴通過讀取application.properties(yml)
內的參數(shù)獲得
@ConfigurationProperties("example.service")
public class ExampleServiceProperties {
private String prefix;
private String suffix;
//省略 getter setter
重點藕赞,編寫AutoConfigure
類
@Configuration
@ConditionalOnClass(ExampleService.class)
@EnableConfigurationProperties(ExampleServiceProperties.class)
public class ExampleAutoConfigure {
@Autowired
private ExampleServiceProperties properties;
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")
ExampleService exampleService (){
return new ExampleService(properties.getPrefix(),properties.getSuffix());
}
}
解釋下用到的幾個和Starter相關的注解:
-
@ConditionalOnClass
,當classpath
下發(fā)現(xiàn)該類的情況下進行自動配置卖局。 -
@ConditionalOnMissingBean
斧蜕,當Spring Context
中不存在該Bean
時。 -
@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")
砚偶,當配置文件中example.service.enabled=true
時批销。
更多相關注解,建議閱讀官方文檔該部分染坯。
最后一步均芽,在resources/META-INF/
下創(chuàng)建spring.factories
文件,內容供參考下面~
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.ExampleAutoConfigure
OK单鹿,完事掀宋,運行 mvn:install
打包安裝,一個Spring Boot Starter便開發(fā)完成了。如果你需要該Starter的源代碼劲妙,點這里湃鹊。
創(chuàng)建一個Spring Boot項目來 試試~
引入example-spring-boot-starter
依賴
<dependency>
<groupId>com.example</groupId>
<artifactId>example-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
創(chuàng)建application.properties
,進行配置
example.service.enabled=true
example.service.prefix=####
example.service.suffix=@@@@
創(chuàng)建一個簡單的Spring Web Application是趴,注入Starter提供的ExampleService
看它能否正常工作~
@SpringBootApplication
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private ExampleService exampleService;
@GetMapping("/input")
public String input(String word){
return exampleService.wrap(word);
}
}
啟動Application涛舍,訪問/input
接口試試看~
總結下Starter
的工作原理
-
Spring Boot
在啟動時掃描項目所依賴的JAR包,尋找包含spring.factories
文件的JAR包 - 根據(jù)
spring.factories
配置加載AutoConfigure
類 - 根據(jù)
@Conditional
注解的條件唆途,進行自動配置并將Bean
注入Spring Context