1.引入依賴
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.d4c.study</groupId>
<artifactId>starter-demo-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
<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>
</project>
因?yàn)槭莝pring Boot啟動(dòng)器,所以要指定parent為:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
spring-boot-configuration-processor
spring-boot-configuration-processor的作用是編譯時(shí)生成 spring-configuration-metadata.json 伪阶,此文件主要給IDE使用。如當(dāng)配置此jar相關(guān)配置屬性在 application.yml ,你可以用ctlr+鼠標(biāo)左鍵點(diǎn)擊屬性名境析,IDE會(huì)跳轉(zhuǎn)到你配置此屬性的類中义矛。
我們?nèi)粘J褂玫腟pring官方的Starter一般采取spring-boot-starter-{name} 的命名方式,如 spring-boot-starter-web 。
而非官方的Starter吝羞,官方建議 artifactId 命名應(yīng)遵循{name}-spring-boot-starter 的格式兰伤。 例如:mybatis-spring-boot-starter 。
spring-boot-autoconfigure
spring-boot-autoconfigure主要是要用到其中的一些注解
2.定義屬性類钧排,用于讀取敦腔,接受配置文件中的值
package com.d4c.starter.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("simple.string")
public class SimpleProperties {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@ConfigurationProperties與@EnableConfigurationProperties配合使用,實(shí)現(xiàn)了配置文件的讀取恨溜,與引入符衔。
3.定義功能實(shí)現(xiàn)類
package com.d4c.starter.autoconfigure;
public class StringResolvedBean {
private String config;
public StringResolvedBean(String config) {
this.config = config;
}
public String[] split(String separatorChar) {
return config.split(separatorChar);
}
}
4.自動(dòng)配置類
package com.d4c.starter.autoconfigure;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(StringResolvedBean.class)
@EnableConfigurationProperties(SimpleProperties.class)
public class SimpleStringAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "simple.string", value = "enabled", havingValue = "true")
StringResolvedBean starterInit(SimpleProperties simpleProperties) {
return new StringResolvedBean(simpleProperties.getValue());
}
}
4.1 有關(guān)條件的注解
@ConditionalOnBean:當(dāng)容器中有指定的Bean的條件下
@ConditionalOnClass:當(dāng)類路徑下有指定的類的條件下
@ConditionalOnExpression:基于SpEL表達(dá)式作為判斷條件
@ConditionalOnJava:基于JVM版本作為判斷條件
@ConditionalOnJndi:在JNDI存在的條件下查找指定的位置
@ConditionalOnMissingBean:當(dāng)容器中沒有指定Bean的情況下
@ConditionalOnMissingClass:當(dāng)類路徑下沒有指定的類的條件下
@ConditionalOnNotWebApplication:當(dāng)前項(xiàng)目不是Web項(xiàng)目的條件下
@ConditionalOnProperty:指定的屬性是否有指定的值
@ConditionalOnResource:類路徑下是否有指定的資源
@ConditionalOnSingleCandidate:當(dāng)指定的Bean在容器中只有一個(gè),或者在有多個(gè)Bean的情況下糟袁,用來(lái)指定首選的Bean @ConditionalOnWebApplication:當(dāng)前項(xiàng)目是Web項(xiàng)目的條件下
5.創(chuàng)建spring.factories
在resources/META-INF/下創(chuàng)建spring.factories文件判族,并添加如下內(nèi)容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.d4c.starter.autoconfigure.SimpleStringAutoConfiguration
6.項(xiàng)目路徑參考
7.打包到本地倉(cāng)庫(kù)
#切到項(xiàng)目根目錄,執(zhí)行mvn命令
mvn install
8.其他本地項(xiàng)目就可以maven dependency引入了
<dependency>
<groupId>com.d4c.study</groupId>
<artifactId>starter-demo-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
application.yml
server:
port: 6011
simple:
string:
enabled: true
value: 12,aa,bb,cc