springboot自定義STARTER執行個體
過程描述:
一、建立一個empty-Project 。然後追加兩個module。一個是maven項目,用于做自動配置的屬性和功能類,另一個是web項目,用來引用autoconfigurer配置工程。

1、在starter的pom.xml中,引用autoconfigurer。
<groupId>com.zhiyuan.starter</groupId>
<artifactId>zhiyuan-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.zhiyuan.starter</groupId>
<artifactId>zhiyuan-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2、自動配置autoconfigurer的pom.xml,并将test相關的引用和build都删掉。
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
com.zhiyuan.starter
zhiyuan-spring-boot-starter-autoconfigurer
0.0.1-SNAPSHOT
zhiyuan-spring-boot-starter-autoconfigurer
Demo project for Spring Boot
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
3、autoconfigurer項目的結構,删除autoconfigurer中的test子產品和相應的配置。
二、開始寫代碼
1、在autoconfigurer工程中,建立TalkServiceAutoconfiguration.java
/@Configuration用于定義配置類,可替換xml配置檔案,
被注解的類内部包含有一個或多個被@Bean注解的方法,這些方法将會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描
,并用于建構bean定義,初始化Spring容器。/
@Configuration
//web應用生效,根據@Conditional不同的條件建立不同的bean。Condition是個接口,需要實作matches方法,傳回true則注入bean,false則不注入。
@ConditionalOnWebApplication
//相當于把使用 @ConfigurationProperties 的類進行了一次注入。
@EnableConfigurationProperties(TalKProperties.class)
public class TalkServiceAutoconfiguration {
@Autowired
TalKProperties talKProperties;
@Bean
public TalkService getTalkService(){
TalkService talkService=new TalkService();
talkService.setTalKProperties(talKProperties);
return talkService;
}
}
2、建立TalKProperties.java檔案
//@ConfigurationProperties 注解,我們可以友善的擷取這些參數值.
@ConfigurationProperties(prefix = “zhiyuan.talk”)
public class TalKProperties {
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;
}
}
3、建立調用類TalkService.java.
public class TalkService {
TalKProperties talKProperties;
public TalKProperties getTalKProperties() {
return talKProperties;
}
public void setTalKProperties(TalKProperties talKProperties) {
this.talKProperties = talKProperties;
}
public String talkToLead(String name) {
return talKProperties.getPrefix() +"----"+name +"-----" + talKProperties.getSuffix();
}
}
4、在META-INFO中建立配置檔案spring.factories。
檔案中配置内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.zhiyuan.starter.demo.TalkServiceAutoconfiguration
5、将autoconfigurer工程,install釋出。starter工程,install釋出
三、建立一個web工程,
1、在pom中引入釋出的starter,并建立類
@RestController
public class TalkController {
@Autowired
TalkService talkService;
@GetMapping("/talk")
public String talk(){
return talkService.talkToLead(“talk to”);
}
}
2,調用結果
四,springboot實作自定義satrter,代碼實作的過程描述。
1、springboot讀取java配置類中的全檔案路徑屬性,也就是放到META-INFO下的一個配置檔案spring.factories配置檔案。
2、根據配置檔案中的配置,加載@Configuration标注的TalkServiceAutoconfiguration自動配置的java類。
3、再在類中@EnableConfigurationProperties标注,注入TalKProperties.java屬性檔案。
4、通過@bean,将建立一個talkService執行個體化到容器。