亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

南昌市| 错那县| 化隆| 子洲县| 惠来县| 平原县| 怀远县| 林芝县| 泗水县| 洛扎县| 铁岭县| 紫金县| 贞丰县| 苏尼特左旗| 天等县| 富锦市| 合肥市| 铜陵市| 通山县| 阳朔县| 西吉县| 榆中县| 普兰县| 哈密市| 康定县| 得荣县| 达州市| 扶沟县| 蛟河市| 赣榆县| 乌审旗| 景宁| 城口县| 浦北县| 星子县| 云林县| 永宁县| 石台县| 姚安县| 瓦房店市| 昭平县|