spring 后处理器
Bean后处理器
新建maven项目并添加spring依赖,目录结构如下
Axe
public interface Axe { public String chop(); }
Person
public interface Person { public void useAxe(); }
SteelAxe
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class SteelAxe implements Axe { public SteelAxe() { System.out.println("Spring实例化依赖bean:SteelAxe实例..."); } public String chop() { return "钢斧砍柴真快"; } }
Chinese
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Chinese implements Person,InitializingBean { private Axe axe; private String name; public Chinese() { System.out.println("Spring 实例化主调bean:Chinese实例..."); } public void setAxe(Axe axe) { this.axe = axe; } public void setName(String name) { System.out.println("Spring执行setName()方法注入依赖关系..."); this.name = name; } public void useAxe(){ System.out.println(name+axe.chop()); } public void init(){ System.out.println("正在执行初始化方法init..."); } public void afterPropertiesSet() throws Exception { System.out.println("正在执行初始化方法afterPropertiesSet..."); } }
MyBeanPostProcessor
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.mythsky.springdemo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.lang.Nullable; public class MyBeanPostProcessor implements BeanPostProcessor { @Nullable public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean后处理器在初始化之前对"+beanName+"进行增强处理..."); return bean; } @Nullable public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean后处理器在初始化之后对"+beanName+"进行增强处理..."); if(bean instanceof Chinese){ Chinese c=(Chinese)bean; c.setName("Hello world!"); } return bean; } }
测试BeanTest
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.mythsky.springdemo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml"); Person p=(Person)ctx.getBean("chinese"); p.useAxe(); } }
services.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="steelAxe" class="org.mythsky.springdemo.SteelAxe"></bean> <bean id="chinese" class="org.mythsky.springdemo.Chinese" init-method="init" p:axe-ref="steelAxe" p:name="依赖注入的值"></bean> <bean class="org.mythsky.springdemo.MyBeanPostProcessor"></bean> </beans>
测试结果
容器后处理器
MyBeanFactoryPostProcessor
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.mythsky.springdemo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { System.out.println("程序对Spring所做的BeanFactory的初始化没有改变..."); System.out.println("Spring容器是:"+configurableListableBeanFactory); } }
services.xml
<bean class="org.mythsky.springdemo.MyBeanFactoryPostProcessor"></bean>
运行上面的测试
PropertyPlaceholderConfigurer
services.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>dbconn.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${jdbc.driverClassName}" p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}"></bean>
dbconn.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://10.200.151.28:3306/spring jdbc.username=root jdbc.password=pass
MysqlTest
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.mythsky.springdemo; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MysqlTest { public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml"); ComboPooledDataSource dataSource= (ComboPooledDataSource) ctx.getBean("dataSource"); System.out.println(dataSource.getDriverClass()); System.out.println(dataSource.getJdbcUrl()); System.out.println(dataSource.getUser()); System.out.println(dataSource.getPassword()); } }
添加c3p0依赖
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.8-dmr</version> </dependency>
运行结果
PropertyOverrideConfigurer
services.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${jdbc.driverClassName}" p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}"></bean> <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> <property name="locations"> <list> <value>db.properties</value> </list> </property> </bean> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"></bean>
db.properties
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
dataSource2.driverClass=com.mysql.jdbc.Driver dataSource2.jdbcUrl=jdbc:mysql://10.200.151.28:3306/spring dataSource2.user=root dataSource2.password=pass
MysqlTest
ComboPooledDataSource dataSource= (ComboPooledDataSource) ctx.getBean("dataSource2");
运行结果同上。
以上两种配置可以简写
<context:property-placeholder location="dbconn.properties"></context:property-placeholder> <context:property-override location="db.properties"></context:property-override>