1. in the Spring framework, org. springframework. beans. factory. config. propertyplaceholderpolicer class can be set. some dynamically set values in the properties (key/value form) file are replaced with values occupying the key ($ key $) in XML ,. the properties file can be customized based on customer needs. Such a design can provide program flexibility.
2. In spring, you can use propertyplaceholderconfigurer to add an external attribute file to the xml configuration file. Of course, you can also specify the encoding of the external file, for example:
<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 attribute files, such:
<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> // write the two values
</List>
</Property>
</Bean>
3. For example, the content of JDBC. properties is:
JDBC. driverclassname = com. MySQL. JDBC. Driver
JDBC. url = JDBC: mysql: // localhost/mysqldb? Useunicode = true & amp; characterencoding = UTF-8 & amp; zerodatetimebehavior = round;
JDBC. Username = root
JDBC. Password = 123456
4. In the spring configuration file, we can write as follows:
<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. It can be seen that the role of propertyplaceholderconfigurer is to put the configuration information of the database to which the placeholder points to the tool defined in bean.
6. Check the source code. We can find that the locations attribute is defined in propertyplaceholderconfigurer's grandfather class propertiesloadersupport, while location only has the setter method. Configurations similar to this are common in spring source programs.