欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年02月07日 16:15:23   作者:漫夭  
這篇文章主要介紹了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)文章

  • Java 基礎(chǔ)之修飾符關(guān)鍵詞整理

    Java 基礎(chǔ)之修飾符關(guān)鍵詞整理

    這篇文章主要介紹了Java 基礎(chǔ)之修飾符關(guān)鍵詞整理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Java9 集合工廠方法解析

    Java9 集合工廠方法解析

    這篇文章主要介紹了Java9 集合工廠方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 使用自定義參數(shù)解析器同一個(gè)參數(shù)支持多種Content-Type

    使用自定義參數(shù)解析器同一個(gè)參數(shù)支持多種Content-Type

    這篇文章主要介紹了使用自定義參數(shù)解析器同一個(gè)參數(shù)支持多種Content-Type的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java利用正則取標(biāo)簽之間的數(shù)據(jù)

    Java利用正則取標(biāo)簽之間的數(shù)據(jù)

    這篇文章主要介紹了Java利用正則取標(biāo)簽之間的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式

    Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式

    這篇文章主要介紹了Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java虛擬機(jī)JVM優(yōu)化實(shí)戰(zhàn)的過(guò)程全記錄

    Java虛擬機(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-08
  • SpringBoot bean依賴屬性配置詳細(xì)介紹

    SpringBoot bean依賴屬性配置詳細(xì)介紹

    Spring容器是Spring的核心,一切SpringBean都存儲(chǔ)在Spring容器內(nèi)??梢哉f(shuō)bean是spring核心中的核心。Bean配置信息定義了Bean的實(shí)現(xiàn)及依賴關(guān)系,這篇文章主要介紹了SpringBoot bean依賴屬性配置
    2022-09-09
  • Java對(duì)數(shù)器驗(yàn)證算法詳解

    Java對(duì)數(shù)器驗(yàn)證算法詳解

    這篇文章主要介紹了Java對(duì)數(shù)器驗(yàn)證算法,Java對(duì)數(shù)函數(shù)的計(jì)算方法非常有問(wèn)題,然而在API中卻有驚人的誤差。但是假如運(yùn)用了以下的方法,用Java處理數(shù)字所碰到的小麻煩就可以輕而易舉的解決了
    2023-04-04
  • MyBatis Log 插件無(wú)法顯示SQL語(yǔ)句的原因解析

    MyBatis 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)題

    這篇文章主要介紹了解決IDEA插件市場(chǎng)Plugins無(wú)法加載的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10

最新評(píng)論