SpringBoot自定義Starter及使用
更新時間:2023年07月25日 10:53:03 作者:「已注銷」
這篇文章主要介紹了SpringBoot自定義Starter及使用,Starter是Spring Boot中的一個非常重要的概念,Starter相當于模塊,它能將模塊所需的依賴整合起來并對模塊內(nèi)的Bean根據(jù)環(huá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: customStarterTestDemo
TestController
@RestController public class TestController { @Resource private ConfigService configService; @GetMapping("/test") public String test() { return configService.showConfig(); } }
3 測試自動裝配
到此這篇關于SpringBoot自定義Starter及使用的文章就介紹到這了,更多相關自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
多模塊maven的deploy集成gitlab?ci自動發(fā)版配置
這篇文章主要為大家介紹了多模塊maven項目deploy集成gitlab?ci自動發(fā)版的配置流程步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02Spring中實現(xiàn)定時調(diào)度的幾種方法
本篇文章主要介紹了Spring中實現(xiàn)定時調(diào)度示例,可以在無人值守的時候系統(tǒng)可以在某一時刻執(zhí)行某些特定的功能,有興趣的可以了解一下。2017-02-02maven <repositories>標簽和<pluginRepositories>標簽的使用
這篇文章主要介紹了maven <repositories>標簽和<pluginRepositories>標簽的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07