上一节,我们已经初步知道了beanPostProcessor这个接口是干什么的了,可以做什么样的事情,并且我们举了一个简单的“Teacher”的例子,但是大家也有点疑问,这个实际意义是什么呢?在项目中实际运用是什么呢?
下面再给出一个例子,在实际项目中,我们一个项目一套代码,有生产环境和开发环境,数据库链接配置文件有2种,jdbc_dev.properties和jdbc_prd.properties这2个配置文件,我们只需要简单的修改一下,spring的配置文件就可以很快的切换数据源,这样支持多环境数据切换,我们就可以用BeanPostProcessor这个接口去实现
代码如下:
DataSourceConnection.java
package org.study.spring.beanpostprocessor; public class DataSourceConnection{ private String driver; private String jdbcName; private String password; private String url; /** * 这里得出数据库链接的各个属性 */ public void initConnection(){ System.out.println("I am database connection ,and I get connection by dirver :"+driver+" and jdbcName :"+jdbcName+" and password:"+password); } public DataSourceConnection() { } public DataSourceConnection(String driver, String jdbcName, String password, String url) { this.driver = driver; this.jdbcName = jdbcName; this.password = password; this.url = url; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getJdbcName() { return jdbcName; } public void setJdbcName(String jdbcName) { this.jdbcName = jdbcName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }ChangeConfigSupportMultiEnvironment.java
package org.study.spring.beanpostprocessor; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class ChangeConfigSupportMultiEnvironment implements BeanPostProcessor{ /** * 目前所属的环境是dev还是prd */ private String devlocation; private String configName; public String getDevlocation() { return devlocation; } public void setDevlocation(String devlocation) { this.devlocation = devlocation; } public String getConfigName() { return configName; } public void setConfigName(String configName) { this.configName = configName; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { /** * 如果该类是DataSourceConnection */ if(bean instanceof DataSourceConnection){ if(configName.contains("${")&&configName.contains("}")){ configName = configName.substring(0,configName.indexOf("${")).concat(devlocation); Properties prop = new Properties(); InputStream in = Object.class.getResourceAsStream("/"+configName+".properties"); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } DataSourceConnection dsc = (DataSourceConnection)bean; String driver = dsc.getDriver(); String jdbcName = dsc.getJdbcName(); String password = dsc.getPassword(); String url = dsc.getUrl(); if(driver.contains("${")&&driver.contains("}")){ driver = driver.replace("${", "").replace("}", ""); } if(jdbcName.contains("${")&&jdbcName.contains("}")){ jdbcName = jdbcName.replace("${", "").replace("}", ""); } if(password.contains("${")&&password.contains("}")){ password = password.replace("${", "").replace("}", ""); } if(url.contains("${")&&url.contains("}")){ url = url.replace("${", "").replace("}", ""); } driver = prop.getProperty(driver); jdbcName = prop.getProperty(jdbcName); password = prop.getProperty(password); url = prop.getProperty(url); return new DataSourceConnection(driver,jdbcName,password,url); } } return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // System.out.println("after..."+this.devlocation); // System.out.println("after..."+this.configName); return bean; } }jdbc_dev.properties
jdbc.driver= com.mysql.jdbc.Driver jdbc.url= jdbc:mysql:/127.0.0.1:3306/gms?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true jdbc.username= dev_user jdbc.password= dev_passwordjdbc_prd.properties
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql:/prd.com:3306/gms?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true jdbc.username = prd_user jdbc.password = prd_user
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <bean id="changeConfigSupportMultiEnvironment" class="org.study.spring.beanpostprocessor.ChangeConfigSupportMultiEnvironment"> <property name="devlocation" value="prd"/> <property name="configName" value="jdbc_${environment}"/> </bean> <bean id="dataSourceConnection" class="org.study.spring.beanpostprocessor.DataSourceConnection"> <property name="driver" value="${jdbc.driver}"/> <property name="jdbcName" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> </bean> </beans>
package org.study.spring.beanpostprocessor; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ChangeConfigSupportMultiEnvironmentTest{ @Test public void test2() throws Exception{ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-post-processor.xml"); DataSourceConnection dataSourceConnection = applicationContext.getBean("dataSourceConnection",DataSourceConnection.class); dataSourceConnection.initConnection(); } }当bean-post-processor.xml中devlocation的值设置为prd,运行截图是:
设置为dev,运行截图是:
这样就可以简单的切换开发环境了,可以根据这个思想,我们可以在开发过程中,灵活的运用spring的BeanPostProcessor~