Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下BeanPostProcessor处理器的使用
先看一下BeanPostProcessor接口的源码,它定义了两个方法,一个在bean初始化之前,一个在bean初始化之后
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
下面,我们来实现这个类,测试一下Spring中的前置后置处理器吧
首先是pom.xml,增加Spring相关的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspring</groupId>
<artifactId>myspring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myspring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring 5.0 核心工具包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- Spring 5.0 Bean管理工具包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- Spring 5.0 context管理工具包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- Spring 5.0 aop支持包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
</dependencies>
</project>
定义一个测试接口:
public interface BaseService {
String doSomething();
String eat();
}
定义接口实现类:
public class ISomeService implements BaseService {
public String doSomething() {
// 增强效果:返回内容全部大写
return "Hello i am kxm";
}
public String eat() {
return "eat food";
}
}
实现BeanPostProcessor接口
public class MyBeanPostProcessor implements BeanPostProcessor {
// 前置处理器
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
Class beanClass = bean.getClass();
if (beanClass == ISomeService.class) {
System.out.println("bean 对象初始化之前······");
}
return bean;
}
// 后置处理器 --- 此处具体的实现用的是Java中的动态代理
public Object postProcessAfterInitialization(final Object beanInstance, String beanName) throws BeansException {
// 为当前 bean 对象注册监控代理对象,负责增强 bean 对象方法的能力
Class beanClass = beanInstance.getClass();
if (beanClass == ISomeService.class) {
Object proxy = Proxy.newProxyInstance(beanInstance.getClass().getClassLoader(),beanInstance.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("ISomeService 中的 doSome() 被拦截了···");
String result = (String) method.invoke(beanInstance, args);
return result.toUpperCase();
}
});
return proxy;
}
return beanInstance;
}
}
Spring的配置文件如下:
<!-- 注册 bean:被监控的实现类 -->
<bean id="iSomeService" class="com.my.spring.beanprocessor.ISomeService"></bean>
<!-- 注册代理实现类 -->
<bean class="com.my.spring.beanprocessor.MyBeanPostProcessor"></bean>
测试类如下:
public class TestBeanPostProcessor {
public static void main(String[] args) {
/**
* BeanPostProcessor 前置后置处理器
*/
ApplicationContext factory = new ClassPathXmlApplicationContext("spring_config.xml");
BaseService serviceObj = (BaseService) factory.getBean("iSomeService");
System.out.println(serviceObj.doSomething());
}
}
测试结果截图:
可以观察到,我们明明在代码中对于doSomething方法定义的是小写,但是通过后置处理器,拦截了原本的方法,而是通过动态代理的方式把方法的结果进行了一定程度的改变,这就是Spring中的前置后置处理器----BeanPostProcessor