How does Spring introduce the properties configuration file?

Source: Internet
Author: User

How does Spring introduce the properties configuration file?
Spring injection of constants can be directly used in java code
Method 1: Use the configuration file Label Configuration
You can configure data of the set, map, list, and properties files. The following describes how to use the properties file:
1. Add applicationContext. xml
Xsi: schemaLocation = "http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

2. Use tags to introduce the properties file path

3. Inject in code
@ Value ("# configEnv ['db _ key']")
Private String db_key;
Tips:
1. You must use spring2.0 or later to support this function.
2. db_key can be Boolean, int, Double, and other basic types.
Method 2: Use PropertyPlaceholderConfigurer to load the configuration file
Class = "org. springframework. beans. factory. config. PropertyPlaceholderConfigurer">


Classpath: app. properties



Then use @ Value ("$ {db_key}") for direct injection.
Tips:
1. Labels can also be used in this mode. To replace the usage:
Ignore-resource-not-found = "true"
Location = "classpath *:/application. properties,
Classpath *:/db. properties "/>
Location can introduce multiple files at a time
2. the prefix of the variable is lost. Therefore, you need to add the prefix to the key of the variable to ensure that the name will not be duplicated. The general format is db. conn. maxConns = 30.
3. This method is more universal and can be directly used in the Spring configuration file. For example:




4. You can also directly load the properties file in the program without using the spring configuration file, for example:
Context = new ClassPathXmlApplicationContext ("applicationContext. xml ");


PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer ();
Cfg. setLocation (new FileSystemResource ("classpath *:/config/util. properties "));
Cfg. setBeanFactory (context );

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.