springboot中@ConfigurationProperties無效果的解決方法
原因有一下幾點(diǎn)
前綴prefix沒寫對(概率不大,但也有)
@Data @ConfigurationProperties(prefix = "message") public class MyBean { private String msg; public MyBean(){ System.out.println("MyBean初始化完成"); } }
2.類的方法中沒有setter
解決方法:
- 用lombok的注解
- 或者自己寫getter setter
3.寫了ConfigurationProperties但是沒寫Component
交給spring管理
4.是用AnnotationConfigApplicationContext(MyBeanConfig.class);獲取的bean不是一個(gè)spring容器(我的問題在于這個(gè))
用spring容器的自動注入是有值的
但自己單開一個(gè)spring容器,這是兩個(gè),所以不會有值,有值的放到了第一個(gè)spring的容器中
第一個(gè)spring容器在
第二個(gè)spring容器是我在測試類開的,
@ConfigurationProperties
在Spring Boot中注解@ConfigurationProperties有三種使用場景。
場景一
使用@ConfigurationProperties和@Component注解到bean定義類上,這里@Component代指同一類實(shí)例化Bean的注解。
基本使用實(shí)例如下:
// 將類定義為一個(gè)bean的注解,比如 @Component,@Service,@Controller,@Repository // 或者 @Configuration @Component // 表示使用配置文件中前綴為user1的屬性的值初始化該bean定義產(chǎn)生的的bean實(shí)例的同名屬性 // 在使用時(shí)這個(gè)定義產(chǎn)生的bean時(shí),其屬性name會是Tom @ConfigurationProperties(prefix = "user1") public class User { private String name; // 省略getter/setter方法 }
對應(yīng)application.properties配置文件內(nèi)容如下:
user1.name=Tom
在此種場景下,當(dāng)Bean被實(shí)例化時(shí),@ConfigurationProperties會將對應(yīng)前綴的后面的屬性與Bean對象的屬性匹配。符合條件則進(jìn)行賦值。
場景二
使用@ConfigurationProperties和@Bean注解在配置類的Bean定義方法上。以數(shù)據(jù)源配置為例:
@Configuration public class DataSourceConfig { @Primary @Bean(name = "primaryDataSource") @ConfigurationProperties(prefix="spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } }
這里便是將前綴為“spring.datasource.primary”的屬性,賦值給DataSource對應(yīng)的屬性值。
@Configuration注解的配置類中通過@Bean注解在某個(gè)方法上將方法返回的對象定義為一個(gè)Bean,并使用配置文件中相應(yīng)的屬性初始化該Bean的屬性。
場景三
使用@ConfigurationProperties注解到普通類,然后再通過@EnableConfigurationProperties定義為Bean。
@ConfigurationProperties(prefix = "user1") public class User { private String name; // 省略getter/setter方法 }
這里User對象并沒有使用@Component相關(guān)注解。
而該User類對應(yīng)的使用形式如下:
@SpringBootApplication @EnableConfigurationProperties({User.class}) public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
上述代碼中,通過@EnableConfigurationProperties對User進(jìn)行實(shí)例化時(shí),便會使用到@ConfigurationProperties的功能,對屬性進(jìn)行匹配賦值。
到此這篇關(guān)于springboot中@ConfigurationProperties無效果的文章就介紹到這了,更多相關(guān)springboot @ConfigurationProperties內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較
- SpringBoot中的@ConfigurationProperties注解解析
- SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
- 關(guān)于SpringBoot的@ConfigurationProperties注解和松散綁定、數(shù)據(jù)校驗(yàn)
- SpringBoot?@Value與@ConfigurationProperties二者有哪些區(qū)別
- springboot如何靜態(tài)加載@configurationProperties
- SpringBoot2底層注解@ConfigurationProperties配置綁定
- Springboot中@ConfigurationProperties輕松管理應(yīng)用程序的配置信息詳解
相關(guān)文章
解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題
這篇文章主要介紹了解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07SpringBoot注解@ConditionalOnClass底層源碼實(shí)現(xiàn)
這篇文章主要為大家介紹了SpringBoot注解@ConditionalOnClass底層源碼實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置
這篇文章主要介紹了SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01java實(shí)現(xiàn)簡單的給sql語句賦值的示例
這篇文章主要介紹了java實(shí)現(xiàn)簡單的給sql語句賦值的示例,需要的朋友可以參考下2014-05-05SpringBoot實(shí)現(xiàn)application配置信息加密
在配置文件中,我們有開發(fā)環(huán)境配置和生產(chǎn)環(huán)境配置,而生產(chǎn)環(huán)境的配置信息是需要做好防護(hù)的,避免外泄,所以本文為大家整理了application配置信息加密的方法,需要的可以參考下2023-07-07Spring事務(wù)失效的一種原因關(guān)于this調(diào)用的問題
這篇文章主要介紹了Spring事務(wù)失效的一種原因關(guān)于this調(diào)用的問題,本文給大家分享問題原因及解決辦法,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10