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 );