@PropertySource 無法讀取配置文件的屬性值解決方案
原來Person類這樣寫: 只寫了@PropertySource注解
@Component @PropertySource(value = {"classpath:person.properties"}) public class Person { private String lastName; private int age; private boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; ... }
運(yùn)行后找不到配置文件中的值:
解決方法:
加上@ConfigurationProperties注解:
@Component @ConfigurationProperties(prefix = "person") @PropertySource(value = {"classpath:person.properties"}) public class Person {
運(yùn)行,獲取到配置文件中的值:
為啥呢??
因?yàn)?@ConfigurationProperties(prefix = “person”)表示該類的屬性值為配置中的屬性值,找前綴為person的屬性。
首先從全局配置文件中找是否有person對(duì)應(yīng)的屬性值,如果有那么就輸出全局配置中的屬性值;如果沒有,@PropertySource意思是屬性來源,從@PropertySource指定的路徑中找到對(duì)應(yīng)的配置文件,進(jìn)行賦值。
@Value和@PropertySource讀配置文件采坑留念
一、 網(wǎng)上查到的方式:
直接 @PropertySource加載文件@Value讀取屬性,
Environment.getProperty()獲取屬性。
結(jié)果發(fā)現(xiàn)@Value只能拿到"${ips}",獲取不到配置文件里的屬性。
@Controller @PropertySource("classpath:queryScoreIPList.properties") public class UserController { @Value("${ips}") private String ips; @Autowired private Environment environment; public void user() { System.err.println(ips); System.err.println(environment.getProperty("ips")); } }
二、 其他方式:
兩種方式加載配置文件,
@Value正常獲取屬性,
Environment.getProperty()獲取不到屬性。
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:queryScoreIPList.properties</value> </list> </property> </bean>
<context:property-placeholder ignore-unresolvable="true" location="classpath:queryScoreIPList.properties" />
三、非要@PropertySource+@Value也可以:
spring配置里裝配PropertySourcesPlaceholderConfigurer或配置context:property-placeholder,
代碼里@PropertySource加載配置文件,
@Value獲取屬性,Environment.getProperty()正常獲取屬性。
<!-- <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"/> --> <context:property-placeholder/>
@Controller @PropertySource("classpath:queryScoreIPList.properties") public class UserController { @Value("${ips}") private String ips; public void user() { System.err.println(ips); } }
總結(jié):
1、@Value從PropertySourcesPlaceholderConfigurer類的PropertySources集合中獲取屬性。
2、PropertySourcesPlaceholderConfigurer初始化時(shí)會(huì)將Environment作為PropertySource放到集合中。
3、@PropertySource注解只加載配置文件到Environment。
4、啟動(dòng)時(shí)@PropertySource注解初始化早于PropertySourcesPlaceholderConfigurer(與@PropertySource所在類和PropertySourcesPlaceholderConfigurer裝配順序無關(guān)),并且@PropertySource加載的配置在xml文件中可以正常獲取。
com.controller.user.UserController.java
@Controller @PropertySource(value="classpath:propList.properties") public class UserController { @Value("${ips}") private String ips; public void user() { System.err.println(ips); System.err.println(environment.getProperty("ips")); } }
applicationContext.xml
<context:component-scan base-package="com.dao"/> <context:component-scan base-package="com.controller"/> <context:property-placeholder location="${location}"/>
propList.properties
location:classpath:queryScoreIPList.properties
以上代碼可以正常獲取queryScoreIPList.properties文件中的ips屬性。希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何開發(fā)基于Netty的HTTP/HTTPS應(yīng)用程序
HTTP/HTTPS是最常見的協(xié)議套件之一,并且隨著智能手機(jī)的成功,它的應(yīng)用也日益廣泛,因?yàn)閷?duì)于任何公司來說,擁有一個(gè)可以被移動(dòng)設(shè)備訪問的網(wǎng)站幾乎是必須的。下面就來看看如何開發(fā)基于Netty的HTTP/HTTPS應(yīng)用程序2021-06-06Javabean基于xstream包實(shí)現(xiàn)轉(zhuǎn)XML文檔的方法
這篇文章主要介紹了Javabean基于xstream包實(shí)現(xiàn)轉(zhuǎn)XML文檔的方法,結(jié)合具體實(shí)例形式分析了xstream包用于轉(zhuǎn)換xml文件的具體使用技巧,需要的朋友可以參考下2017-05-05IDEA 程序包不存在,找不到符號(hào)但是明明存在對(duì)應(yīng)的jar包(問題分析及解決方案)
這篇文章主要介紹了IDEA 程序包不存在,找不到符號(hào)但是明明存在對(duì)應(yīng)的jar包 的解決方案,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Java導(dǎo)出Word文檔的實(shí)現(xiàn)方法詳解
這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出Word文檔的實(shí)現(xiàn)方法,在日常的開發(fā)工作中,我們時(shí)常會(huì)遇到導(dǎo)出Word文檔報(bào)表的需求,比如公司的財(cái)務(wù)報(bào)表、醫(yī)院的患者統(tǒng)計(jì)報(bào)表、電商平臺(tái)的銷售報(bào)表等等,需要的朋友可以參考下2023-08-08Struts1簡介和入門_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Struts1簡介和入門的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09SpringBoot實(shí)現(xiàn)自定義Redis的連接的流程步驟
Spring Boot 自定義 Redis 主要是指在基于 Spring Boot 的應(yīng)用程序中,當(dāng)你需要更深入地控制或擴(kuò)展對(duì) Redis 數(shù)據(jù)庫的操作,而不是僅僅依賴 Spring Data Redis 的默認(rèn)配置,本文給大家介紹了SpringBoot實(shí)現(xiàn)自定義Redis的連接的流程步驟,需要的朋友可以參考下2024-09-09java實(shí)現(xiàn)一個(gè)簡單TCPSocket聊天室功能分享
這篇文章主要為大家分享了java實(shí)現(xiàn)的一個(gè)簡單TCPSocket聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04Jenkins+Docker+Gitee+SpringBoot自動(dòng)化部署
本文主要介紹了Jenkins+Docker+Gitee+SpringBoot自動(dòng)化部署,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03