BeanPostProcessor 结构图
1 code 如下:
package com.gientech.resolveBeforeInstantiation;
public class BeforeInstantiation {
public void doSomething(){
System.out.println("do some thing ......");
}
}
package com.gientech.resolveBeforeInstantiation;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("目标方法执行之前: " + method);
Object o1 = methodProxy.invokeSuper(o, objects);
System.out.println("目标方法执行之后: " + method);
return o1;
}
}
BeanPostProcessor 接口中有postProcessBeforeInitialization,postProcessAfterInitialization 两个方法,
InstantiationAwareBeanPostProcessor 接口中有postProcessBeforeInstantiation,postProcessAfterInstantiation,postProcessProperties 方法
MyInstantiationAwareBeanPostProcessor 继承了InstantiationAwareBeanPostProcessor 接口,所以 在MyInstantiationAwareBeanPostProcessor 中需要实现5个方法。
package com.gientech.resolveBeforeInstantiation;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.cglib.proxy.Enhancer;
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("beanName:"+beanName+"----执行postProcessBeforeInitialization方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("beanName:"+beanName+"----执行postProcessAfterInitialization方法");
return bean;
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("beanName:"+beanName+"----执行postProcessBeforeInstantiation方法");
if (beanClass == BeforeInstantiation.class){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanClass);
enhancer.setCallback(new MyMethodInterceptor());
BeforeInstantiation beforeInstantiation = (BeforeInstantiation) enhancer.create();
System.out.println("创建代理对象: " + beforeInstantiation);
return beforeInstantiation;
}
return beanClass;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return false;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
return pvs;
}
}
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beforeInstantiation" class="com.gientech.resolveBeforeInstantiation.BeforeInstantiation"></bean>
<bean id="myInstantiationAwareBeanPostProcessor" class="com.gientech.resolveBeforeInstantiation.MyInstantiationAwareBeanPostProcessor"></bean>
</beans>
package com.gientech.resolveBeforeInstantiation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeforeInstantiationTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("BeforeInstantiation.xml");
BeforeInstantiation bean = ac.getBean(BeforeInstantiation.class);
bean.doSomething();
}
}
运行结果如下
创建完成后Bean在内存中的名称如下