中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring無法讀取properties文件數據問題詳解

發布時間:2020-10-11 11:34:35 來源:腳本之家 閱讀:626 作者:漫夭 欄目:編程語言

1. controller中無法讀取config.properties文件

controller中注入的@Value配置是從servlet-context.xml配置文件中獲取的;service中注入的@Value配置可以從applicationContext.xml中獲取的。所以,如果要在controller中注入屬性配置,需要在相應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中無法讀取config.properties文件

查看配置文件是否有多個。如果配置的路徑是classpath:config.properties, 鼠標點擊文件。如果顯示”multiple implementations”, 表示有多個文件,查看其他的文件中是否有需要的配置項,沒有的話,很可能就是加載了其他文件的配置項。這時,將路徑改為classpath*:config.properties即可。

<context:property-placeholder
   ignore-unresolvable="true" location="classpath:/jdbc.properties, classpath*:/config.properties"/>

查看日志,發現:

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

加載了兩個config.properties文件。

3.關于診斷:

1)首先確認是否正確加載了配置文件。查看日志:

正常日志如下:

[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 

如果文件沒有加載,則查看路徑是否匹配等。

2)如果文件加載ok,查看配置屬性是否正確加載。

查看tomcat啟動的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]

更多關于spring讀取properties文件數據的文章請查看下面的相關文章

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

库尔勒市| 黄平县| 池州市| 敦化市| 桂平市| 南漳县| 东兴市| 阳东县| 克东县| 山阳县| 南昌市| 信宜市| 金湖县| 平阴县| 昌图县| 东至县| 从江县| 崇明县| 上饶县| 黄石市| 恩施市| 武宣县| 资中县| 普宁市| 桂林市| 南平市| 富锦市| 吉林市| 榆树市| 剑川县| 文登市| 东台市| 新营市| 从化市| 西丰县| 清镇市| 新余市| 边坝县| 鄂尔多斯市| 土默特右旗| 尼玛县|