SpringBoot中的@EnableConfigurationProperties注解詳細(xì)解析
1.概述
@EnableConfigurationProperties注解的作用是:使 使用 @ConfigurationProperties 注解的類生效。
如果一個(gè)配置類只配置@ConfigurationProperties注解,而沒(méi)有使用@Component或者實(shí)現(xiàn)了@Component的其他注解,那么在IOC容器中是獲取不到properties 配置文件轉(zhuǎn)化的bean。說(shuō)白了 @EnableConfigurationProperties 相當(dāng)于把使用 @ConfigurationProperties 的類進(jìn)行了一次注入。
簡(jiǎn)單點(diǎn)說(shuō)@EnableConfigurationProperties的功能類似于@Component。
2.測(cè)試
2.1 使用 @EnableConfigurationProperties 進(jìn)行注冊(cè)
@ConfigurationProperties(prefix = "service.properties")
public class HelloServiceProperties {
private static final String SERVICE_NAME = "test-service";
private String msg = SERVICE_NAME;
set/get
}
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
}
@RestController
public class ConfigurationPropertiesController {
@Autowired
private HelloServiceProperties helloServiceProperties;
@RequestMapping("/getObjectProperties")
public Object getObjectProperties () {
System.out.println(helloServiceProperties.getMsg());
return myConfigTest.getProperties();
}
}配置文件application.properties
service.properties.name=my-test-name service.properties.ip=192.168.1.1 service.user=kayle service.port=8080
一切正常,但是 HelloServiceAutoConfiguration 頭部不使用 @EnableConfigurationProperties,測(cè)訪問(wèn)報(bào)錯(cuò)。
2.2 使用 @Component 注冊(cè)
不使用 @EnableConfigurationProperties 進(jìn)行注冊(cè),使用 @Component 注冊(cè)
@ConfigurationProperties(prefix = "service.properties")
@Component
public class HelloServiceProperties {
private static final String SERVICE_NAME = "test-service";
private String msg = SERVICE_NAME;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}Controller 不變,一切正常,如果注釋掉 @Component 測(cè)啟動(dòng)報(bào)錯(cuò)。 由此證明,兩種方式都是將被 @ConfigurationProperties 修飾的類,加載到 Spring Env 中。
3.項(xiàng)目中的使用場(chǎng)景
如下,在配置類NacosConfigAutoConfiguration的頭上加注解@EnableConfigurationProperties(NacosConfigProperties.class),
而在NacosConfigProperties配置類本身并沒(méi)有實(shí)現(xiàn)了@Component相關(guān)的注解,也就是說(shuō)運(yùn)行項(xiàng)目時(shí),不會(huì)直接把NacosConfigProperties配置類注入到Spring 容器中,而是在執(zhí)行NacosConfigAutoConfiguration這個(gè)配置類時(shí)才會(huì)去把NacosConfigProperties類注入到spring

如下,NacosConfigProperties類本身并沒(méi)有@Component相關(guān)注解:

到此這篇關(guān)于SpringBoot中的@EnableConfigurationProperties注解詳細(xì)解析的文章就介紹到這了,更多相關(guān)@EnableConfigurationProperties注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringData JPA中查詢接口Repository的使用
本文主要介紹了SpringData JPA中查詢接口Repository的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java正則匹配HTML中a標(biāo)簽里的中文字符示例
這篇文章主要介紹了java正則匹配HTML中a標(biāo)簽里的中文字符,涉及java中文正則及HTML元素操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
java之TreeUtils生成一切對(duì)象樹(shù)形結(jié)構(gòu)案例
這篇文章主要介紹了java之TreeUtils生成一切對(duì)象樹(shù)形結(jié)構(gòu)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
JDK1.8使用的垃圾回收器和執(zhí)行GC的時(shí)長(zhǎng)以及GC的頻率方式
這篇文章主要介紹了JDK1.8使用的垃圾回收器和執(zhí)行GC的時(shí)長(zhǎng)以及GC的頻率方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
關(guān)于Spring MVC框架中攔截器Interceptor的使用解讀
這篇文章主要介紹了關(guān)于Spring MVC框架中攔截器Interceptor的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring數(shù)據(jù)庫(kù)連接池url參數(shù)踩坑及解決
這篇文章主要介紹了Spring數(shù)據(jù)庫(kù)連接池url參數(shù)踩坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
盤(pán)點(diǎn)SpringBoot中@Async注解的遇到的坑點(diǎn)及解決辦法
SpringBoot是一個(gè)流行的Java開(kāi)發(fā)框架,在異步編程方面,Spring Boot提供了@Async注解,它能夠讓方法異步執(zhí)行,然而,在使用@Async注解時(shí),有一些潛在的坑需要注意,本文將深入探討Spring Boot中使用@Async注解時(shí)可能遇到的8大坑點(diǎn),并提供相應(yīng)的解決方案2024-03-03

