- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- springboot微服務(wù)自定義starter原理的示例分析
這篇文章主要介紹了springboot微服務(wù)自定義starter原理的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
使用spring boot開(kāi)發(fā)微服務(wù)后,工程的數量大大增加(一定要按照領(lǐng)域來(lái)切,不要一個(gè)中間件客戶(hù)端包一個(gè)),讓各個(gè)jar從開(kāi)發(fā)和運行時(shí)自包含成了一個(gè)重要的內容之一。spring boot starter就可以用來(lái)解決該問(wèn)題(沒(méi)事啟動(dòng)時(shí)別依賴(lài)于applicationContext.getBean獲取bean進(jìn)行處理,依賴(lài)關(guān)系太折騰,有時(shí)候在復雜系統中解決此事比較麻煩,需要修改開(kāi)源框架代碼才能實(shí)現,反過(guò)來(lái)修改開(kāi)源源碼后,維護也是個(gè)麻煩事)。言歸正傳,說(shuō)說(shuō)自定義starter。首先請熟悉spring boot的核心理念,不然容易為了starter而starter,這種情況太多了。
創(chuàng )建一個(gè)maven項目,在pom文件中添加如下依賴(lài):
<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屬性類(lèi),用于讀取屬性(當然可選,如果一開(kāi)始沒(méi)有按照spring boot autoconfig的套路來(lái),改起來(lái)還是挺費勁的,但是一旦這么做了,就會(huì )想,TMD這才是真正的開(kāi)發(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配置此注解可以自動(dòng)導入application.properties配置文件中的屬性,前提需要指定屬性前綴prefix。
3.創(chuàng )建配置類(lèi)
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 )建自動(dòng)配置類(lèi):
@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文件夾下面新建一個(gè)META-INF文件,并在下面創(chuàng )建spring.factories文件,將4中的配置類(lèi)進(jìn)行注冊。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.HelloServiceAutoConfiguration
6.新建一個(gè)springboot項目,在pom文件中添加剛剛打包的jar的坐標。
7.使用@Autowired訪(fǎng)問(wèn)接口。
@SpringBootApplication@RestControllerpublic 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(); }}
相比原來(lái)要使用@Import注解導入一個(gè)@Configuration類(lèi),或者在一處集中維護ComponentScan的所有路徑,使用autoconfigure starter可以讓?xiě)妹黠@實(shí)現的更加自包含和解耦。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站