溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何解決spring無(wú)法讀取properties文件數(shù)據(jù)問(wèn)題

發(fā)布時(shí)間:2021-10-15 15:54:56 來(lái)源:億速云 閱讀:156 作者:柒染 欄目:編程語(yǔ)言

本篇文章為大家展示了如何解決spring無(wú)法讀取properties文件數(shù)據(jù)問(wèn)題,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

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 下載地址

加載了兩個(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 下載地址

 異常日志如下:

[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 existorg.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]

上述內(nèi)容就是如何解決spring無(wú)法讀取properties文件數(shù)據(jù)問(wèn)題,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI