您好,登錄后才能下訂單哦!
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>
查看配置文件是否有多個。如果配置的路徑是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文件。
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文件數據的文章請查看下面的相關文章
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。