程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> spring無法讀取properties文件數據,springproperties

spring無法讀取properties文件數據,springproperties

編輯:JAVA綜合教程

spring無法讀取properties文件數據,springproperties


只講述異常點,關於怎麼配置文件,這裡不做說明。  

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]
如果配置屬性沒有正確加載,查看屬性配置的key是否正確。 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved