SpringBoot創(chuàng)建自定義Starter代碼實(shí)例
自定義SpringBoot Starter
引入項(xiàng)目的配置依賴
<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中配置
//指定項(xiàng)目在屬性文件中配置的前綴為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類
自動(dòng)配置類,用于完成Bean創(chuàng)建等工作
// 定義 java 配置類
@Configuration
//引入DemoService
@ConditionalOnClass({DemoService.class})
// 將 application.properties 的相關(guān)的屬性字段與該類一一對(duì)應(yīng),并生成 Bean
@EnableConfigurationProperties(DemoProperties.class)
public class DemoAutoConfiguration {
// 注入屬性類
@Autowired
private DemoProperties demoProperties;
@Bean
// 當(dāng)容器沒有這個(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ù)此文件來加載項(xiàng)目的自動(dòng)化配置類
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>
編寫屬性配置文件
#配置自定義的屬性信息 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代碼實(shí)例的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解
這篇文章主要介紹了spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java8 Collectors求和功能的自定義擴(kuò)展操作
這篇文章主要介紹了Java8 Collectors求和功能的自定義擴(kuò)展操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
IntelliJ IDEA編譯項(xiàng)目報(bào)錯(cuò) "xxx包不存在" 或 "找不到符號(hào)"
這篇文章主要介紹了IntelliJ IDEA編譯項(xiàng)目報(bào)錯(cuò) "xxx包不存在" 或 "找不到符號(hào)" ,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
java括號(hào)匹配算法求解(用棧實(shí)現(xiàn))
這篇文章主要介紹了java括號(hào)匹配算法求解(用棧實(shí)現(xiàn)),需要的朋友可以參考下2020-12-12
java中i18n的properties的亂碼問題及解決思路
解決java后臺(tái)返回給前端的500信息亂碼問題,通過開啟IDEA的Transparent native-to-ascii conversion編碼,使讀取i18n配置文件的信息顯示正常,此方法僅供參考2026-05-05
Springboot項(xiàng)目啟動(dòng)時(shí)端口被占用的問題及解決
這篇文章主要介紹了Springboot項(xiàng)目啟動(dòng)時(shí)端口被占用的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05

