When configuring beans in a configuration file, it is sometimes necessary to mix the details of the system deployment in the bean's configuration (for example: file path, data source configuration information, etc.). And these deployment details actually need to be separated from the Bean configuration
Spring provides a propertyplaceholderconfigurer Beanfactory post processor that allows the user to move part of the Bean configuration out of the properties file. You can use the form ${var} in the Bean configuration file to assign a value to a variable, propertyplaceholderconfigurer a dependency file, and use these properties to replace the variable.
Spring also allows the use of ${propname} in property files to implement mutual references between attributes.
The bean uses the external properties file implementation steps (to configure the C3P0 data source as an example)
1 Create c3p0 External properties file Db.properties (using key-value pairs)
User=rootpassword=1230driverclass=com.mysql.jdbc.driverjdbcurl=jdbc:mysql://localhost:3306/test
After 2spring4.0 is configured using the context namespace, you need to import the context namespace:
3 Configure the external file in the configuration file, and the bean instance of C3P0, with the following code:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <Context:property-placeholder Location= "Com/jeremy/spring/properties/c3po.properties"/> <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> < Propertyname= "User"value= "${user}"></ Property> < Propertyname= "Password"value= "${password}"></ Property> < Propertyname= "Driverclass"value= "${driverclass}"></ Property> < Propertyname= "Jdbcurl"value= "${jdbcurl}"></ Property> </Bean></Beans>
Spring---Bean uses external properties file