Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that spring cannot read properties file data

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to solve the problem that spring can not read properties file data, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Config.properties file cannot be read in controller

The @ Value configuration injected in controller is obtained from the servlet-context.xml configuration file; the @ Value configuration injected in service can be obtained from applicationContext.xml. So, if you want to inject the property configuration in controller, you need to add the configuration in the appropriate servlet file, just like in applicationContext.xml.

Classpath:jdbc.properties classpath:config.properties

Unable to read config.properties file in 2.service

Check to see if there are multiple configuration files. If the configured path is classpath:config.properties, click on the file. If "multiple implementations" is displayed, which indicates that there are multiple files, check to see if there are any required configuration items in other files. If not, it is likely that the configuration items of other files have been loaded. At this point, change the path to classpath*:config.properties.

Check the log and find:

[2017-01-05 16:45:02 INFO] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?)-Loading properties file from URL [org.springframework.context.support.PropertySourcesPlaceholderConfigurer:? org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?]-Loading properties file from URL [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?)-download address for download address of [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?)-Loading properties file from URL download address

Two config.properties files are loaded.

3. About diagnostics:

1) first verify that the configuration file is loaded correctly. View the log:

The normal log is as follows:

[2017-01-05 16:45:02 INFO] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?)-Loading properties file from URL download address

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 to see if the path matches and so on.

2) if the file loads ok, check to see if the configuration properties are loaded correctly.

View the debug log started by tomcat:

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]

The above content is how to solve the problem that spring can not read properties file data. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report