要在instantiation之前做一些操作
public class BeforeInstantiation {
public void doSomeThing(){
System.out.println("执行do some thing....");
}
}
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
@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 new BeforeInstantiation();
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("beanName:"+beanName+"----执行postProcessAfterInstantiation方法");
return false;
}
@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 PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
System.out.println("beanName:"+beanName+"----执行postProcessProperties方法");
return pvs;
}
}
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("resolveBeforeInstantiation.xml");
}
}
<?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.mashibing.resolveBeforeInstantiation.BeforeInstantiation"></bean>
<bean id="myInstantiationAwareBeanPostProcessor" class="com.mashibing.resolveBeforeInstantiation.MyInstantiationAwareBeanPostProcessor"></bean>
</beans>
我们执行一下看一下refresh方法的registerBeanPostProcessors方法怎么走
一直到这一步,我们没看到创建对象。因为我们写了一个bpp,所以我们这里点进registerBeanPostProcessor, 对于要注册的myInstantiationAwareBeanPostProcessor,因为我们还没有创建这个对象,所以我们会先走创建流程,这里我们从createbean方法开始看起,前面的流程和以前一样
点进这个方法
@Nullable
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
Object bean = null;
// 如果beforeInstantiationResolved值为null或者true,那么表示尚未被处理,进行后续的处理
if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
// Make sure bean class is actually resolved at this point.
// 确认beanclass确实在此处进行处理
// 判断当前mbd是否是合成的,只有在实现aop的时候synthetic的值才为true,并且是否实现了InstantiationAwareBeanPostProcessor接口
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
// 获取类型
Class<?> targetType = determineTargetType(beanName, mbd);
if (targetType != null) {
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
// 是否解析了
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
}
!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)
mbd.beforeInstantiationResolved字段的含义就是是否需要在实例化之前解析
!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()
!mbd.isSynthetic():如果bean不是合成的,合成bean的含义就是spring的内部bean,不是我们创建的对象
hasInstantiationAwareBeanPostProcessors()
具体方法,可以看到是查看这个字段的值。值为false
protected boolean hasInstantiationAwareBeanPostProcessors() {
return this.hasInstantiationAwareBeanPostProcessors;
}
这里为false.往下走
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
因为到这里只走了判断条件,没有实际执行代码,针对mbd.beforeInstantiationResolved = (bean != null);,因为bean还是为null,所以没有给mbd.beforeInstantiationResolved赋值,返回空的bean
到这里还没有myInstantiationAwareBeanPostProcessor对象,我们继续往下走创建对象
resolveBeforeInstantiation方法走完,继续往下走, 调用doCreateBean创建对象,返回对象实例。
注册好了以后我们就继续往下走,不做其他操作了。
走到onfresh的finishBeanFactoryInitialization,走到这里我们就开始创建我们写的一些对象,比如beforeInstantiation,同样要走创建对象的流程,前面的步骤省略,走到resolveBeforeInstantiation
点进去
@Nullable
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
Object bean = null;
// 如果beforeInstantiationResolved值为null或者true,那么表示尚未被处理,进行后续的处理
if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
// Make sure bean class is actually resolved at this point.
// 确认beanclass确实在此处进行处理
// 判断当前mbd是否是合成的,只有在实现aop的时候synthetic的值才为true,并且是否实现了InstantiationAwareBeanPostProcessor接口
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
// 获取类型
Class<?> targetType = determineTargetType(beanName, mbd);
if (targetType != null) {
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
// 是否解析了
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
}
!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)
mbd.beforeInstantiationResolved还没设置,为null,所以进入,
!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()
对象不是合成的
因为我们再之前注册BPP的时候注册过myInstantiationAwareBeanPostProcessor,所以这里有beanpostprocessor,继续进入
Class<?> targetType = determineTargetType(beanName, mbd);
获取目标类型,可以看到这里返回的是一个Class
这里返回的targetType部位空,继续进入
从方法名可以看出是在实例化之前应用BPP,在初始化之前应用BPP
@Nullable
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
}
return null;
}
可以看到BPP有我们写的 myInstantiationAwareBeanPostProcessor
点进去,调用到我们重写的方法
调用到我们写的postProcessorBeforeInstantiation,并为mbd.beforeInstantiationResolved设置值,返回bean
往下走,返回一个BeforeInstantiation 代理对象。