????????Spring boot之所以流行,是因為Spring starter谓晌。Spring starter是Spring boot的核心掠拳,可以理解為一個可拔插式的插件,例如纸肉,你想使用Reids插件溺欧,那么可以使用spring-boot-starter-redis;如果想使用MongoDB柏肪,可以使用spring-boot-starter-data-mongodb姐刁。
????????如果我要使用redis,我直接引入redis驅(qū)動jar包就行了烦味,何必要引入starter包聂使?starter和普通jar包的區(qū)別在于壁拉,它能夠?qū)崿F(xiàn)自動配置,和Spring Boot無縫銜接柏靶,從而節(jié)省我們大量開發(fā)時間弃理。下面通過制作一個Starter的jar包來了解Spring starter是如何讓Spring的開發(fā)更加簡單方便的。
1屎蜓,Starter的命名
????????官方對Starter項目的jar包定義的 artifactId 是有要求的痘昌,當(dāng)然也可以不遵守。Spring官方Starter通常命名為spring-boot-starter-{name}如:spring-boot-starter-web炬转,Spring官方建議非官方的starter命名應(yīng)遵守{name}-spring-boot-starter的格式辆苔。
2,Maven依賴
????????Spring starter讓spring boot簡單扼劈,主要是引入了自動配置驻啤,所以這里需要引入自動配置相關(guān)的依賴。
<dependencies>
? ? <dependency>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-configuration-processor</artifactId>
? ? ? ? <optional>true</optional>
? ? </dependency>
? ? <dependency>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-autoconfigure</artifactId>
? ? </dependency>
</dependencies>
????????其中 spring-boot-configuration-processor 的作用是編譯時生成 spring-configuration-metadata.json 测僵,此文件主要給IDE使用街佑。如當(dāng)配置此jar相關(guān)配置屬性在 application.properties或者applicationg.yml ,你可以用ctlr+鼠標(biāo)左鍵點擊屬性名捍靠,IDE會跳轉(zhuǎn)到你配置此屬性的類中。
3,編寫Service類
????????這里講一下我們的Starter要實現(xiàn)的功能森逮,很簡單榨婆,提供一個Service,包含一個能夠?qū)⑴渲梦募信渲玫淖址鶕?jù)傳入的字符進行分割的方法String[] split(String separatorChar)褒侧。
public class StarterService {
? ? private String config;
? ? public StarterService(String config) {
? ? ? ? this.config = config;
? ? }
? ? public String[] split(String separatorChar) {
? ? ? ? return StringUtils.split(this.config, separatorChar);
? ? }
4良风,編寫配置文件讀取類
????????配置文件讀取類一般命名為XxxProperties,該配置類主要用于接收Spring boot中application.properties或者application.yml的配置項闷供,主要代碼如下:
@ConfigurationProperties(prefix = "starter.test")
public class StarterTestProperties {
? private String config;?
? ? public void setConfig(String config) {
? ? ? ? this.config = config;
? ? }
? ? public String getConfig() {
? ? ? ? return config;
? ? }
}
5烟央,編寫AutoConfigure類(關(guān)鍵)
@Configuration
@ConditionalOnClass(StarterService.class)
@EnableConfigurationProperties(StarterTestProperties.class)
public class StarterAutoConfigure {
? ? @Autowired
? ? private StarterTestProperties properties;
? ? @Bean
? ? @ConditionalOnMissingBean
? ? @ConditionalOnProperty(prefix = "example.service", value = "enabled", havingValue = "true")
? ? StarterService starterService (){
? ? ? ? return new StarterService(properties.getConfig());
? ? }
}
說明以下代碼中的幾個注解:
@ConditionalOnClass,當(dāng)classpath下發(fā)現(xiàn)該類的情況下進行自動配置歪脏。
@ConditionalOnMissingBean疑俭,當(dāng)Spring 容器中不存在該Bean時創(chuàng)建該bean。
@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")婿失,當(dāng)配置文件中example.service.enabled=true時钞艇,創(chuàng)建該bean。
6豪硅,創(chuàng)建spring.factories
????????在resources/META-INF/下創(chuàng)建spring.factories文件哩照,并添加如下內(nèi)容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.StarterAutoConfigure
7,編譯jar包
????????將maven工程編譯成jar包懒浮。
8飘弧,創(chuàng)建spring boot工程,引入jar包
????????創(chuàng)建spring boot工程,并引入剛才生成的starter jar包次伶。
9蹋岩,測試
在application.properties 配置文件中添加配置信息:
example.service.enabled=true
starter.test=yyyy,xxx,lll,zzz
單元測試用例如下:
@Autowired
private StarterService starterService;
@Test
public void starterTest() {
? ? String[] splitArray = starterService.split(",");
? ? System.out.println(splitArray);
}
????一個簡單的Starter就創(chuàng)建完成了,從一個簡單的starter創(chuàng)建過程学少,就可以看出spring boot是如何簡化我們的項目開發(fā)的剪个。