SpringBoot創(chuàng)建自定義Starter代碼實例
自定義SpringBoot Starter
引入項目的配置依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.4.RELEASE</version> </dependency>
創(chuàng)建xxxService類
完成相關(guān)的操作邏輯
DemoService.java @Data public class DemoService{ private String str1; private String str2; }
定義xxxProperties類
屬性配置類,完成屬性配置相關(guān)的操作,比如設(shè)置屬性前綴,用于在application.properties中配置
//指定項目在屬性文件中配置的前綴為str,即可以在屬性文件中通過 str.str1=springboot,就可以改變屬性類字段 str1 的值了 @SuppressWarnings("ConfigurationProperties") @ConfigurationProperties(prefix = "str") @Data public class DemoProperties { public static final String DEFAULT_STR1 = "SpringBoot "; public static final String DEFAULT_STR2 = "Starter"; private String str1 = DEFAULT_STR1; private String str2 = DEFAULT_STR2; }
定義xxxAutoConfiguration類
自動配置類,用于完成Bean創(chuàng)建等工作
// 定義 java 配置類 @Configuration //引入DemoService @ConditionalOnClass({DemoService.class}) // 將 application.properties 的相關(guān)的屬性字段與該類一一對應(yīng),并生成 Bean @EnableConfigurationProperties(DemoProperties.class) public class DemoAutoConfiguration { // 注入屬性類 @Autowired private DemoProperties demoProperties; @Bean // 當(dāng)容器沒有這個 Bean 的時候才創(chuàng)建這個 Bean @ConditionalOnMissingBean(DemoService.class) public DemoService helloworldService() { DemoService demoService= new DemoService(); demoService.setStr1(demoProperties.getStr1()); demoService.setStr2(demoProperties.getStr2()); return demoService; } }
在resources下創(chuàng)建目錄META-INF
在 META-INF 目錄下創(chuàng)建 spring.factories,在SpringBoot啟動時會根據(jù)此文件來加載項目的自動化配置類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.springboot.config.DemoAutoConfiguration
其他項目中使用自定義的Starter
<!--引入自定義Starter--> <dependency> <groupId>com.lhf.springboot</groupId> <artifactId>spring-boot-starter-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
編寫屬性配置文件
#配置自定義的屬性信息 str.str1=str1 str.str2=str2
寫注解使用
@RestController public class StringController { @Autowired private DemoService demoService; //引入自定義Starter中的DemoService @RequestMapping("/") public String addString(){ return demoService.getStr1()+ demoService.getStr2(); } }
到此這篇關(guān)于SpringBoot創(chuàng)建自定義Starter代碼實例的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android?Studio?中Gradle配置sonarqube插件(推薦)
Sonarqube作為一個很實用的靜態(tài)代碼分析工具,在很多項目中都使用,本文重點給大家介紹Android?Studio?中Gradle配置sonarqube插件的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧2022-03-03BootStrap Jstree 樹形菜單的增刪改查的實現(xiàn)源碼
這篇文章主要介紹了BootStrap Jstree 樹形菜單的增刪改查的實現(xiàn)源碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02Java如何解決發(fā)送Post請求報Stream?closed問題
這篇文章主要介紹了Java如何解決發(fā)送Post請求報Stream?closed問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06通過FeignClient調(diào)用微服務(wù)提供的分頁對象IPage報錯的解決
這篇文章主要介紹了通過FeignClient調(diào)用微服務(wù)提供的分頁對象IPage報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java的Lambda表達(dá)式和Stream流的作用以及示例
這篇文章主要介紹了Java的Lambda表達(dá)式和Stream流簡單示例,Lambda允許把函數(shù)作為一個方法的參數(shù),使用Lambda表達(dá)式可以寫出更簡潔、更靈活的代碼,而其作為一種更緊湊的代碼風(fēng)格,使Java的語言表達(dá)能力得到了提升,需要的朋友可以參考下2023-05-05