spring無(wú)法讀取properties文件數(shù)據(jù)問(wèn)題詳解
1. controller中無(wú)法讀取config.properties文件
controller中注入的@Value配置是從servlet-context.xml配置文件中獲取的;service中注入的@Value配置可以從applicationContext.xml中獲取的。所以,如果要在controller中注入屬性配置,需要在相應(yīng)servlet文件中添加配置,同applicationContext.xml中一樣。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:config.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean>
2.service中無(wú)法讀取config.properties文件
查看配置文件是否有多個(gè)。如果配置的路徑是classpath:config.properties, 鼠標(biāo)點(diǎn)擊文件。如果顯示”multiple implementations”, 表示有多個(gè)文件,查看其他的文件中是否有需要的配置項(xiàng),沒(méi)有的話,很可能就是加載了其他文件的配置項(xiàng)。這時(shí),將路徑改為classpath*:config.properties即可。
<context:property-placeholder ignore-unresolvable="true" location="classpath:/jdbc.properties, classpath*:/config.properties"/>
查看日志,發(fā)現(xiàn):
[2017-01-05 16:45:02 INFO ] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?) - Loading properties file from URL [jar:file:/home/admin/creative-task/lib/xxxx-common-1.5.7.jar!/config.properties] [2017-01-05 16:45:02 INFO ] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?) - Loading properties file from URL [file:/home/admin/creative-task/conf/config.properties]
加載了兩個(gè)config.properties文件。
3.關(guān)于診斷:
1)首先確認(rèn)是否正確加載了配置文件。查看日志:
正常日志如下:
[2017-01-05 16:45:02 INFO ] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?) - Loading properties file from URL [file:/home/admin/creative-task/conf/config.properties]
異常日志如下:
[2017-01-05 16:39:39 ERROR] [main] (Main:22) - Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist
如果文件沒(méi)有加載,則查看路徑是否匹配等。
2)如果文件加載ok,查看配置屬性是否正確加載。
查看tomcat啟動(dòng)的debug日志:
正常日志如下:
[2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [environmentProperties] [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemProperties] [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemEnvironment] [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Could not find key 'adx.id' in any property source. Returning [null] [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [localProperties] [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Found key 'adx.id' in [localProperties] with type [String] and value '1'
異常日志如下:
[2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [environmentProperties] [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemProperties] [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemEnvironment] [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Could not find key 'adx.id' in any property source. Returning [null] [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [localProperties] [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Could not find key 'adx.id' in any property source. Returning [null]
更多關(guān)于spring讀取properties文件數(shù)據(jù)的文章請(qǐng)查看下面的相關(guān)文章
相關(guān)文章
使用自定義參數(shù)解析器同一個(gè)參數(shù)支持多種Content-Type
這篇文章主要介紹了使用自定義參數(shù)解析器同一個(gè)參數(shù)支持多種Content-Type的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java利用正則取標(biāo)簽之間的數(shù)據(jù)
這篇文章主要介紹了Java利用正則取標(biāo)簽之間的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式
這篇文章主要介紹了Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java虛擬機(jī)JVM優(yōu)化實(shí)戰(zhàn)的過(guò)程全記錄
有人說(shuō)Java之所以能夠崛起,JVM功不可沒(méi)。Java虛擬機(jī)最初服務(wù)于讓Java語(yǔ)言凌駕于平臺(tái)之上,實(shí)現(xiàn)“編寫(xiě)一次,到處運(yùn)行”,那么下面這篇文章主要給大家分享了個(gè)關(guān)于Java虛擬機(jī)JVM優(yōu)化實(shí)戰(zhàn)的過(guò)程全記錄,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08MyBatis Log 插件無(wú)法顯示SQL語(yǔ)句的原因解析
MyBatis Log是IDEA一款下載量非常高的插件,該插件可以對(duì)控制臺(tái)打印的日志進(jìn)行解析,然后將對(duì)應(yīng)的SQL語(yǔ)句整理并拼接好對(duì)應(yīng)的參數(shù),非常方便。這篇文章給大家介紹MyBatis Log 插件無(wú)法顯示SQL語(yǔ)句的原因,感興趣的朋友跟隨小編一起看看吧2020-09-09解決IDEA插件市場(chǎng)Plugins無(wú)法加載的問(wèn)題
這篇文章主要介紹了解決IDEA插件市場(chǎng)Plugins無(wú)法加載的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10