I. foreword
Decentralized configuration idea: create a properties file, add data, configure the properties file in the beans file first, and then use placeholders to reference data in beans
- For many processing interfaces in the bean's life cycle, the processing method is spring's own bean, that is, spring's special bean
2. When the property file is imported through context: Property placeholder, there are multiple required, number interval
The 3.beans file uses placeholders to reference the contents of the properties file: eg.${key}
II. Decentralized configuration
Through the self built properties file, configure the key value, read it in the spring configuration file, and realize the decentralized configuration
db.properties file
username=admin password=123456 driver=com.ahd.www
Configuration file beans:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- Introducing our db.properties file --> <!-- One way:To configure bean <bean class="PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>com/ahd/dispatcher/db.properties</value> </list> </property> </bean> --> <!-- Mode two: --> <context:property-placeholder location="classpath:com/ahd/dispatcher/db.properties"></context:property-placeholder> <bean id="dbutil" class="com.ahd.dispatcher.DBUtil"> <property name="username"><value>${username}</value></property> <property name="password"><value>${password}</value></property> <property name="drivername"><value>${driver}</value></property> </bean> </beans>
DBUtil class:
DBUtil.javapackage com.ahd.dispatcher; public class DBUtil { private String username; private String password; private String drivername; public DBUtil() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDrivername() { return drivername; } public void setDrivername(String drivername) { this.drivername = drivername; } }