Spring entry (2) [load the properties file], springproperties
Configuration information must be used during the development process, which cannot be hard-coded. In this case, the configuration file is a good method. java provides properties files, save the information as a key-Value Pair and obtain the corresponding key value during reading. spring provides support for reading the properties file. The following describes the specific configuration,
1. <context: property-placeholder location = ""/> label
Database connections are often used in projects. parameters such as driver, url, username, and password are required to connect to the database. These parameters are usually stored in the properties file, for example, the src directory of my project contains db. properties file, file content,
db.driver=com.mysql.jdbc.Driverdb.url=jdbc:mysql://192.168.8.32:3306/testdb.username=rootdb.passwd=root
Now we need to use spring to load this file. Let's see how spring is configured. The spring configuration file contains <context: property-placeholder location = "classpath: db. properties "/> label, which supports loading a single file, but can use wildcards, such as classpath: db *. properties. The db under src is specified here. properties file. How can I use the loaded file? $ {db. driver}. The following is an example used in the database connection pool,
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${db.driver}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.passwd}"></property> </bean>
In the spring configuration file, you can use the above method to obtain the value in the db. properties file,
Now I want to use db in java class. the Value in the properties file can be @ Value ("$ {db. driver} "), do not assign values to attributes in the class using such an annotation, as shown below:
package com.cn.study.day2;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class Configuration { @Value("${db.driver}") private String dbDriver; @Value("${db.url}") private String dbUrl; @Value("${db.username}") private String dbUsername; @Value("${db.passwd}") private String dbPasswd; public String getDbDriver() { return dbDriver; } public String getDbUrl() { return dbUrl; } public String getDbUsername() { return dbUsername; } public String getDbPasswd() { return dbPasswd; } }
Annotations are used in attributes or the setXXX method. The following is the test code,
// Cpac is an instance object Configuration con = (Configuration) cpac of ClassPathXmlApplicationContext. getBean ("configuration"); System. out. println (con. getDbDriver (); System. out. println (con. getDbUrl (); System. out. println (con. getDbUsername (); System. out. println (con. getDbPasswd ());
The output is as follows,
com.mysql.jdbc.Driverjdbc:mysql://192.168.8.32:3306/testrootroot
The preceding example shows how to use the properties file in a java class.
Ii. propertyplaceholderpolicer class
Configure the bean of the propertyplaceholderpolicer class
<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:quartz.properties</value> <value>classpath:db.properties</value>
</array> </property></bean>
You can use this method to specify multiple files. You only need to configure multiple <value> labels. This method is used with <context: property-placeholder location = "classpath: db. properties "/> is the same. The usage method is the same as the first one.
<Context: property-placeholder location = "classpath: db. properties "/> and the method for configuring the PropertyPlaceholderConfigurer class have only one function in the spring configuration file. That is, if both are configured, only the first configuration will be loaded into the configuration file, because the spring container uses the reflection scanning mechanism, if there is already an instance of the PropertyPlaceholderConfigurer class, it will not be created any more, so you can use one of the methods.
Iii. PropertiesFactoryBean class
You can configure this class as follows,
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:quartz.properties</value>
<value>classpath:db.properties</value>
</list> </property> </bean>
To load the properties file in this way, use # {beanId ['key']}, as shown above, # {configProperties ['db. url ']} You can use this method to obtain the db. url value,
For example, the database is connected,
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${db.driver}</value> </property> <property name="url"> <value>#{configProperties['db.url']}</value> </property> <property name="username"> <value>#{configProperties['db.username']}</value> </property> <property name="password"> <value>123456</value> </property> </bean>
The preceding method uses both $ {db. driver} and # {configProperties ['db. url']}. Both methods are acceptable,
In java code, annotation @ Value is also used, but the Value is # {configProperties ['db. url ']}, Z @ Value ("# {configProperties ['db. url ']} "), as follows,
@Value("#{configProperties['db.username']}") private String dbUsername;
This annotation can also be used in the setXXX method.
The above are three ways for spring to load the properties file. Someone may ask why the locations and location attributes are used during configuration. Below is a UML class diagram about properties,
This class diagram illustrates the inheritance relationships between classes. locations and location are defined in the same parent class PropertiesLoaderSupport. This is an abstract class.
Well, there is so much content about spring loading properties. The source code has to be further studied.
If any error exists, thank you!