SpringBoot自定義Starter及使用
更新時間:2023年07月25日 10:53:03 作者:「已注銷」
這篇文章主要介紹了SpringBoot自定義Starter及使用,Starter是Spring Boot中的一個非常重要的概念,Starter相當(dāng)于模塊,它能將模塊所需的依賴整合起來并對模塊內(nèi)的Bean根據(jù)環(huán)境進(jìn)行自動配置,需要的朋友可以參考下
1 創(chuàng)建一個自定義Starter項目

CustomConfig
@Configuration
@EnableConfigurationProperties(value = CustomProperties.class) // 使配置類生效
@ConditionalOnProperty(prefix = "mmw.config", name = "enable", havingValue = "true") // 自動裝配條件
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)建一個測試springBoot項目

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 測試自動裝配

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

