spring boot微服務自定義starter原理詳解
這篇文章主要介紹了spring boot微服務自定義starter原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
使用spring boot開發(fā)微服務后,工程的數量大大增加(一定要按照領域來切,不要一個中間件客戶端包一個),讓各個jar從開發(fā)和運行時自包含成了一個重要的內容之一。spring boot starter就可以用來解決該問題(沒事啟動時別依賴于applicationContext.getBean獲取bean進行處理,依賴關系太折騰,有時候在復雜系統(tǒng)中解決此事比較麻煩,需要修改開源框架代碼才能實現,反過來修改開源源碼后,維護也是個麻煩事)。言歸正傳,說說自定義starter。首先請熟悉spring boot的核心理念,不然容易為了starter而starter,這種情況太多了。
創(chuàng)建一個maven項目,在pom文件中添加如下依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
創(chuàng)建properties屬性類,用于讀取屬性(當然可選,如果一開始沒有按照spring boot autoconfig的套路來,改起來還是挺費勁的,但是一旦這么做了,就會想,TMD這才是真正的開發(fā)模式,@Value那套早該丟了)。
@ConfigurationProperties(prefix = "com.xxx")
public class HelloServiceProperties {
private String name = "james";
private String hobby = "cc";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
@ConfigurationProperties配置此注解可以自動導入application.properties配置文件中的屬性,前提需要指定屬性前綴prefix。
3.創(chuàng)建配置類
public class HelloService {
private String name;
private String hobby;
public String getName() {
return "name is " + name;
}
public String getHobby() {
return "hobby is " + hobby;
}
public void setName(String name) {
this.name = name;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
4.創(chuàng)建自動配置類:
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloServiceConfiguration.class)
@ConditionalOnProperty(prefix = "com.xxx", value = "enabled", matchIfMissing = true)@ComponentScan({"com.xxx"}) // 如果bean比較多,一般采用這種方式
public class HelloServiceAutoConfiguration {
@Autowired
private HelloServiceProperties helloServiceProperties;
@Bean // bean比較少、且順序和邏輯有特殊要求的模塊,一般采用這種方式
@ConditionalOnMissingBean(HelloServiceConfiguration.class)
public HelloServiceConfiguration helloServiceConfiguration() {
HelloService helloService = new HelloService();
helloService.setName(helloServiceProperties.getName());
helloService.setHobby(helloServiceProperties.getHobby());
return helloService;
}
}
5.在resources文件夾下面新建一個META-INF文件,并在下面創(chuàng)建spring.factories文件,將4中的配置類進行注冊。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.HelloServiceAutoConfiguration
6.新建一個springboot項目,在pom文件中添加剛剛打包的jar的坐標。
7.使用@Autowired訪問接口。
@SpringBootApplication
@RestController
public class Springboot03Application {
@Autowired
private HelloService helloService;
public static void main(String[] args) {
SpringApplication.run(Springboot03Application.class, args);
}
@RequestMapping("/name")
public String getName() {
return helloService.getName();
}
@RequestMapping("/hobby")
public String getHobby() {
return helloService.getHobby();
}
}
相比原來要使用@Import注解導入一個@Configuration類,或者在一處集中維護ComponentScan的所有路徑,使用autoconfigure starter可以讓應用明顯實現的更加自包含和解耦。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
RabbitMQ,RocketMQ,Kafka?事務性,消息丟失,消息順序性和消息重復發(fā)送的處理策略問題
這篇文章主要介紹了RabbitMQ,RocketMQ,Kafka?事務性,消息丟失,消息順序性和消息重復發(fā)送的處理策略,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
SpringBoot?Profile多環(huán)境配置方式
這篇文章主要介紹了SpringBoot?Profile多環(huán)境配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
MapStruct內部錯誤:NullPointerException的解決方案
在Java開發(fā)中,MapStruct是一個非常流行的對象映射工具,它通過注解處理器在編譯時生成映射代碼,極大地簡化了對象之間的轉換操作,本文將詳細分析一個常見的MapStruct內部錯誤——NullPointerException,并提供一系列解決方案,需要的朋友可以參考下2025-02-02

