Spring cannot read the properties file data. springproperties

Source: Internet
Author: User

Spring cannot read the properties file data. springproperties

Only the exception points are described. We will not describe how to configure the file here. 1. the config. the @ Value configuration injected in the properties file controller is obtained from the servlet-context.xml configuration file; the @ Value configuration injected in the service can be obtained from applicationContext. obtained in xml. Therefore, if you want to inject attribute configuration in the controller, you need to add the configuration in the corresponding servlet File, the same as in 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. the config. properties file cannot be read from the service to check whether there are multiple configuration files. If the configuration path is classpath: config. properties, click the file. If "multiple implementations" is displayed, it indicates that there are multiple files to check whether there are any required configuration items in other files. If not, it is likely that the configuration items of other files are loaded. Change the path to classpath *: config. properties.
<context:property-placeholder      ignore-unresolvable="true" location="classpath:/jdbc.properties, classpath*:/config.properties"/>

View logs and find:

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

Two config. properties files are loaded.

3. Diagnosis: 1) check whether the configuration file is correctly loaded. View logs: the normal logs are as follows:
[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]

The exception log is as follows:

[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 existorg.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 
If the file is not loaded, check whether the path matches. 2) If the file is loaded, check whether the configuration attributes are correctly loaded. View the debug log of tomcat startup: the normal log is as follows:
[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'
The exception log is as follows:
[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]
If the configuration property is not correctly loaded, check whether the key configured for the property is correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.