Reprint Address:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html
Spring leverages Propertyplaceholderconfigurer placeholders
1. Propertyplaceholderconfigurer is the implementation of a bean factory post processor, which is an implementation of the Beanfactorypostprocessor interface. Propertyplaceholderconfigurer can place attribute values in a context (configuration file) in another separate standard Java properties file. Replaces the value in the specified properties file with ${key} in the XML file. In this case, only the properties file needs to be modified, not the XML configuration file.
2. In spring, you can use Propertyplaceholderconfigurer to include an external property file in an XML configuration file, or you can specify the encoding of an external file, such as:
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Location" > <value>conf/sqlmap/jdbc.properties</value> </ property> <property name= "fileencoding" > <value>UTF-8</value> </property ></bean>
Of course, you can also introduce multiple properties files, such as:
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Locations" > <list> <value>/web-inf/mail.properties</value > <value>classpath:conf/sqlmap/jdbc.properties</value>//Note the notation for these two value values </list> </property></bean>
3. For example, the contents of Jdbc.properties are:
jdbc.driverclassname=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost/mysqldb?useunicode=true& characterencoding=utf-8&zerodatetimebehavior=round;jdbc.username=rootjdbc.password=123456
4. In the spring configuration file, we can write this:
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Locations" > <list> <value>classpath:conf/sqlmap/ Jdbc.properties </value> </list> </property></bean><bean id= "DataSource" class = "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "Driverclassname" Value= "${jdbc.driverclassname}"/> <property name= "url" value= "${jdbc.url}"/> <property name= " Username "value=" ${jdbc.username} "/> <property name=" password "value=" ${jdbc.password} "/></bean >
5. In this way, a simple data source is set up. As you can see, the role of Propertyplaceholderconfigurer is to place the database configuration information pointed to by the placeholder in the tool defined in the bean.
6. Looking at the source code, you can see that the locations attribute is defined in Propertyplaceholderconfigurer's grandfather class Propertiesloadersupport, and location is only the setter method. Similar to this configuration, it is common in spring source programs.
Propertyplaceholderconfigurer if the property you want to use is not found in the specified properties file, it is also found in the Java System Class properties.
We can pass parameters to the spring configuration file via System.setproperty (key, value) or Java via-dnamevalue.
Spring's Propertyplaceholderconfigurer Application