How spring references the configuration in the properties file
Source: Internet
Author: User
1.PropertyPlaceholderConfigurer class
It replaces the defined variable (var) in the attribute with a placeholder for ${var} in the spring's configuration file
< beans >
< Bean id = "Configbean" class = "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< name = "Location" > < value > db.properties </value > </property >
</Bean >
< Bean id = "DataSource" class = "Org.apache.commons.dbcp.BasicDataSource" Destroy-method = "Close" >
< property name = ' Driverclassname ' >< value > ${jdbc.driverclassname} </value ></property >
< property name = ' URL ' >< value > ${jdbc.url} </value ></property >
< property name = ' username ' >< value > ${jdbc.username} </value ></property >
< property name = ' password ' >< value > ${jdbc.password} </value ></property >
</Bean >
</Beans > Db.properties files
Jdbc.driverclassname=org.hsqldb.jdbcdriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
Jdbc.username=sa
Jdbc.password=root 2.PropertyOverrideConfigurer Class
Same as Propertyplaceholderconfigurer function, but the usage is different. Do not use placeholders, in the properties file
Directly define the value of the property, which allows for a default value
< beans >
< Bean id = "Configbean" class = "Org.springframework.beans.factory.config.PropertyOverrideConfigurer" >
< property name = ' Location ' >< value > Db.properties </value ></Property >
</Bean >
< Bean id = "DataSource" class = "Org.apache.commons.dbcp.BasicDataSource" Destroy-method = "Close" >
< property name = ' Driverclassname ' >< value > Org.hsqldb.jdbcDriver </value ></Property >
< property name = ' URL ' >< value > jdbc:hsqldb:hsql://production:9002 </value ></Property >
< property name = ' username ' >< value >test </value ></Property >
< property name = ' Password ' >< value >123456 </value ></Property >
</Bean >
</Beans > Db.properties files
Datasource.username=admin
datasource.password=9527 when the bean instance is in place, admin,9527 replaces the test,123456
3 other
1 If you need to refer to multiple properties, change the Configbean property to
< property name = "Locations" >
< list >
< value > db.properties </value >
< value > db1.properties </value >
</list >
</Property > 2) automatically invokes the Beanfactorypostprocessor interface in Applactioncontext and must be manually added if it is to be used in Beanfactory:
Xmlbeanfactory factory = new Xmlbeanfactory (New Filesystemresource ("Beans.xml"));
Propertyplaceholderconfigurer cfg = new Propertyplaceholderconfigurer ();
Cfg.setlocation (New Filesystemresource ("Jdbc.properties"));
Cfg.postprocessbeanfactory (Factory);
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.