Spring uses propertyplaceholderconfigurer placeholders

Source: Internet
Author: User

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.

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.