sping @Bean创建对象四 BeanPostProcessor方法的执行以及BeanPostProcessor的初始化

继续上一篇说创建对象之后会执行initializeBean(beanName, exposedObject, mbd); 具体内容如下

        //执行BeanPostProcessor.postProcessBeforeInitialization()
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);

        //执行init方法
        invokeInitMethods(beanName, wrappedBean, mbd);

        //执行BeanPostProcessor.postProcessAfterInitialization()
        wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

然后接着看下applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);这个方法

@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
        throws BeansException {

    Object result = existingBean;
    for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
        result = beanProcessor.postProcessBeforeInitialization(result, beanName);
        if (result == null) {
            return result;
        }
    }
    return result;
}

就是循环BeanPostProcessor列表,然后执行其postProcessBeforeInitialization(result, beanName);方法,没什么好说的,接着看下getBeanPostProcessors()这个方法(获取BeanPostProcessor列表)

public List getBeanPostProcessors() {
    return this.beanPostProcessors;
}

getBeanPostProcessors()方法很简单,就是直接返回beanPostProcessors,是在AbstractBeanFactory.java的一个属性

private final List beanPostProcessors = new ArrayList();


然后就是继续试着看下,这个数组是怎么初始化的了,同样的,找到该数组的add方法。然后debug

sping @Bean创建对象四 BeanPostProcessor方法的执行以及BeanPostProcessor的初始化_第1张图片

1.同样的执行AbstractApplicationContext.refresh()方法

2.执行AbstractApplicationContext.refresh()方法中的registerBeanPostProcessors(beanFactory);

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

3.执行PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);

public static void registerBeanPostProcessors(
    ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
	//获取所有的BeanPostProcessor的名字
	String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

	// Register BeanPostProcessorChecker that logs an info message when
	// a bean is created during BeanPostProcessor instantiation, i.e. when
	// a bean is not eligible for getting processed by all BeanPostProcessors.
	int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
	beanFactory.addBeanPostProcessor(new PostProcessorRegistrationDelegate.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

	// Separate between BeanPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	List priorityOrderedPostProcessors = new ArrayList();
	List internalPostProcessors = new ArrayList();
	List orderedPostProcessorNames = new ArrayList();
	List nonOrderedPostProcessorNames = new ArrayList();
	//遍历postProcessorNames,并创建BeanPostProcessor对象,分类放入到不同的数组中
	for (String ppName : postProcessorNames) {
	    if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		priorityOrderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
		    internalPostProcessors.add(pp);
		}
	    }
	    else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
		orderedPostProcessorNames.add(ppName);
	    }
	    else {
		nonOrderedPostProcessorNames.add(ppName);//SomePostProcessor就在这个数组中
	    }
	}

	// First, register the BeanPostProcessors that implement PriorityOrdered.
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

	// Next, register the BeanPostProcessors that implement Ordered.
	List orderedPostProcessors = new ArrayList();
	for (String ppName : orderedPostProcessorNames) {
	    BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
	    orderedPostProcessors.add(pp);
	    if (pp instanceof MergedBeanDefinitionPostProcessor) {
		internalPostProcessors.add(pp);
	    }
	}
	sortPostProcessors(orderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, orderedPostProcessors);

	// Now, register all regular BeanPostProcessors.
	List nonOrderedPostProcessors = new ArrayList();
	for (String ppName : nonOrderedPostProcessorNames) {
	    BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
	    nonOrderedPostProcessors.add(pp);
	    if (pp instanceof MergedBeanDefinitionPostProcessor) {
		internalPostProcessors.add(pp);
	    }
	}
	//执行到这里,注册BeanPostProcessor
	registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

	// Finally, re-register all internal BeanPostProcessors.
	sortPostProcessors(internalPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, internalPostProcessors);

	// Re-register post-processor for detecting inner beans as ApplicationListeners,
	// moving it to the end of the processor chain (for picking up proxies etc).
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

4.执行registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors); 循环postProcessors

private static void registerBeanPostProcessors(
        ConfigurableListableBeanFactory beanFactory, List postProcessors) {

    for (BeanPostProcessor postProcessor : postProcessors) {
        beanFactory.addBeanPostProcessor(postProcessor);
    }
}

5.执行beanFactory.addBeanPostProcessor(postProcessor);

public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
    Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
    this.beanPostProcessors.remove(beanPostProcessor);
    this.beanPostProcessors.add(beanPostProcessor);
    if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
        this.hasInstantiationAwareBeanPostProcessors = true;
    }
    if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
        this.hasDestructionAwareBeanPostProcessors = true;
    }
}

 

postProcessAfterInitialization应该也差不多,就不再看了

 


刚开始的对这两行代码感到很奇怪,为什么添加之前先移除?

this.beanPostProcessors.remove(beanPostProcessor);
this.beanPostProcessors.add(beanPostProcessor);

关于这点还是要看PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);这个方法中在分类BeanPostProcessor时的代码,一共四类,四个数组

List priorityOrderedPostProcessors = new ArrayList();
List internalPostProcessors = new ArrayList();
List orderedPostProcessorNames = new ArrayList();
List nonOrderedPostProcessorNames = new ArrayList();

再看下四个分类的代码,

for (String ppName : postProcessorNames) {
    if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
	//第一个分类,priorityOrderedPostProcessors
        priorityOrderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {//由于这个if条件的存在,第一个分类和第二个分类可能会存在交集
	    //第二个分类,internalPostProcessors
            internalPostProcessors.add(pp);
        }
    }
    else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
	//第三个分类 orderedPostProcessorNames
        orderedPostProcessorNames.add(ppName);
    }
    else {
	//第四个分类 nonOrderedPostProcessorNames
        nonOrderedPostProcessorNames.add(ppName);
    }
}

priorityOrderedPostProcessors和internalPostProcessors可能存在交集,同样的情况在处理orderedPostProcessors,nonOrderedPostProcessors时也可以见到

List orderedPostProcessors = new ArrayList();
    for (String ppName : orderedPostProcessorNames) {
    BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
    orderedPostProcessors.add(pp);
    if (pp instanceof MergedBeanDefinitionPostProcessor) {
        internalPostProcessors.add(pp);
    }
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);

// Now, register all regular BeanPostProcessors.
List nonOrderedPostProcessors = new ArrayList();
    for (String ppName : nonOrderedPostProcessorNames) {
    BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
    nonOrderedPostProcessors.add(pp);
    if (pp instanceof MergedBeanDefinitionPostProcessor) {
        internalPostProcessors.add(pp);
    }
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

由于internalPostProcessors可能与其他三个分类出现交集,而四个分类添加顺序是

1.priorityOrderedPostProcessors

2.orderedPostProcessors

3.nonOrderedPostProcessors

4.internalPostProcessors

所以在添加之前要删除以下,一方面时避免重复,第二是,可以确保internalPostProcessors集和的元素放到最后

 


当然了,这上面是自己定义的BeanPostProcessor。还有其他的一些

sping @Bean创建对象四 BeanPostProcessor方法的执行以及BeanPostProcessor的初始化_第2张图片

你可能感兴趣的:(spring)