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

