SpringBoot自定義Starter及使用
1 創(chuàng)建一個(gè)自定義Starter項(xiàng)目

CustomConfig
@Configuration
@EnableConfigurationProperties(value = CustomProperties.class) // 使配置類(lèi)生效
@ConditionalOnProperty(prefix = "mmw.config", name = "enable", havingValue = "true") // 自動(dòng)裝配條件
public class CustomConfig {
@Resource
private CustomProperties customProperties;
@Bean
public ConfigService defaultCustomConfig() {
return new ConfigService(customProperties.getAge(), customProperties.getName(), customProperties.getInfo());
}
}CustomProperties
@ConfigurationProperties(prefix = "mmw.config")
public class CustomProperties {
private Integer age;
private String name;
private String info;
// getter/setter
}ConfigService
public class ConfigService {
private Integer age;
private String name;
private String info;
public ConfigService(Integer age, String name, String info) {
this.age = age;
this.name = name;
this.info = info;
}
public String showConfig() {
return "ConfigService{" +
"age=" + age +
", name='" + name + '\'' +
", info='" + info + '\'' +
'}';
}
}META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.mmw.demo.config.CustomConfig
2 創(chuàng)建一個(gè)測(cè)試springBoot項(xiàng)目

application.yml
mmw:
config:
enable: true
age: 26
name: 'my custom starter'
info: 'custom web info...'
server:
port: 8081
spring:
application:
name: customStarterTestDemoTestController
@RestController
public class TestController {
@Resource
private ConfigService configService;
@GetMapping("/test")
public String test() {
return configService.showConfig();
}
}3 測(cè)試自動(dòng)裝配

到此這篇關(guān)于SpringBoot自定義Starter及使用的文章就介紹到這了,更多相關(guān)自定義Starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)表分月存儲(chǔ)
本文主要介紹了MyBatis實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)表分月存儲(chǔ),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
關(guān)于SpringMVC中控制器如何處理文件上傳的問(wèn)題
這篇文章主要介紹了關(guān)于SpringMVC中控制器如何處理文件上傳的問(wèn)題,在 Web 應(yīng)用程序中,文件上傳是一個(gè)常見(jiàn)的需求,例如用戶(hù)上傳頭像、上傳文檔等,本文將介紹 Spring MVC 中的控制器如何處理文件上傳,并提供示例代碼,需要的朋友可以參考下2023-07-07
Java遠(yuǎn)程調(diào)試保姆級(jí)教程(附詳細(xì)圖文)
這篇文章主要介紹了Java遠(yuǎn)程調(diào)試的相關(guān)資料,Java遠(yuǎn)程調(diào)試是一種在本地計(jì)算機(jī)上調(diào)試部署在遠(yuǎn)程服務(wù)器上的Java應(yīng)用程序的能力,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-07-07
Java 使用Docker時(shí)經(jīng)常遇到的五個(gè)問(wèn)題
這篇文章主要介紹了Java 使用Docker時(shí)經(jīng)常遇到的五個(gè)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-10-10

