Spring IoC源码学习:registerBeanPostProcessors 详解

// a bean is created during BeanPostProcessor instantiation, i.e. when

// a bean is not eligible for getting processed by all BeanPostProcessors.

// BeanPostProcessor的目标计数

int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;

// 2.添加BeanPostProcessorChecker(主要用于记录信息)到beanFactory中

beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

// Separate between BeanPostProcessors that implement PriorityOrdered,

// Ordered, and the rest.

// 3.定义不同的变量用于区分: 实现PriorityOrdered接口的BeanPostProcessor、实现Ordered接口的BeanPostProcessor、普通BeanPostProcessor

// 3.1 priorityOrderedPostProcessors: 用于存放实现PriorityOrdered接口的BeanPostProcessor

List priorityOrderedPostProcessors = new ArrayList();

// 3.2 internalPostProcessors: 用于存放Spring内部的BeanPostProcessor

List internalPostProcessors = new ArrayList();

// 3.3 orderedPostProcessorNames: 用于存放实现Ordered接口的BeanPostProcessor的beanName

List orderedPostProcessorNames = new ArrayList();

// 3.4 nonOrderedPostProcessorNames: 用于存放普通BeanPostProcessor的beanName

List nonOrderedPostProcessorNames = new ArrayList();

// 4.遍历postProcessorNames, 将BeanPostProcessors按3.1 - 3.4定义的变量区分开

for (String ppName : postProcessorNames) {

if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {

// 4.1 如果ppName对应的Bean实例实现了PriorityOrdered接口, 则拿到ppName对应的Bean实例并添加到priorityOrderedPostProcessors

BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);

priorityOrderedPostProcessors.add(pp);

if (pp instanceof MergedBeanDefinitionPostProcessor) {

// 4.2 如果ppName对应的Bean实例也实现了MergedBeanDefinitionPostProcessor接口,

// 则将ppName对应的Bean实例添加到internalPostProcessors

internalPostProcessors.add(pp);

}

}

else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {

// 4.3 如果ppName对应的Bean实例没有实现PriorityOrdered接口, 但是实现了Ordered接口, 则将ppName添加到orderedPostProcessorNames

orderedPostProcessorNames.add(ppName);

}

else {

// 4.4 否则, 将ppName添加到nonOrderedPostProcessorNames

nonOrderedPostProcessorNames.add(ppName);

}

}

// First, register the BeanPostProcessors that implement PriorityOrdered.

// 5.首先, 注册实现PriorityOrdered接口的BeanPostProcessors

// 5.1 对priorityOrderedPostProcessors进行排序

sortPostProcessors(priorityOrderedPostProcessors, beanFactory);

// 5.2 注册priorityOrderedPostProcessors

registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

// Next, register the BeanPostProcessors that implement Ordered.

// 6.接下来, 注册实现Ordered接口的BeanPostProcessors

List orderedPostProcessors = new ArrayList();

for (String ppName : orderedPostProcessorNames) {

// 6.1 拿到ppName对应的BeanPostProcessor实例对象

BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);

// 6.2 将ppName对应的BeanPostProcessor实例对象添加到orderedPostProcessors, 准备执行注册

orderedPostProcessors.add(pp);

if (pp instanceof MergedBeanDefinitionPostProcessor) {

// 6.3 如果ppName对应的Bean实例也实现了MergedBeanDefinitionPostProcessor接口,

// 则将ppName对应的Bean实例添加到internalPostProcessors

internalPostProcessors.add(pp);

}

}

// 6.4 对orderedPostProcessors进行排序

sortPostProcessors(orderedPostProcessors, beanFactory);

// 6.5 注册orderedPostProcessors

registerBeanPostProcessors(beanFactory, orderedPostProcessors);

// Now, register all regular BeanPostProcessors.

// 7.注册所有常规的BeanPostProcessors(过程与6类似)

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);

// Finally, re-register all internal BeanPostProcessors.

// 8.最后, 重新注册所有内部BeanPostProcessors(相当于内部的BeanPostProcessor会被移到处理器链的末尾)

// 8.1 对internalPostProcessors进行排序

sortPostProcessors(internalPostProcessors, beanFactory);

// 8.2注册internalPostProcessors

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).

// 9.重新注册ApplicationListenerDetector(跟8类似,主要是为了移动到处理器链的末尾)

beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));

}

5.1 对 priorityOrderedPostProcessors进行排序,在 Spring IoC:invokeBeanFactoryPostProcessors解析 代码中3已经解析过。

5.2 注册 priorityOrderedPostProcessors,见代码块2详解

代码块2:registerBeanPostProcessors


private static void registerBeanPostProcessors(

ConfigurableListableBeanFactory beanFactory, List postProcessors) {

// 1.遍历postProcessors

for (BeanPostProcessor postProcessor : postProcessors) {

// 2.将PostProcessor添加到BeanFactory中的beanPostProcessors缓存

beanFactory.addBeanPostProcessor(postProcessor);

}

}

2.将 PostProcessor 添加到 BeanFactory 中的 beanPostProcessors 缓存,见代码块3详解

代码块3:beanFactory.addBeanPostProcessor


@Override

public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {

Assert.notNull(beanPostProcessor, “BeanPostProcessor must not be null”);

// 1.如果beanPostProcessor已经存在则移除(可以起到排序的效果,beanPostProcessor可能本来在前面,移除再添加,则变到最后面)

this.beanPostProcessors.remove(beanPostProcessor);

// 2.将beanPostProcessor添加到beanPostProcessors缓存

this.beanPostProcessors.add(beanPostProcessor);

if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {

// 3.如果beanPostProcessor是InstantiationAwareBeanPostProcessor, 则将hasInstantiationAwareBeanPostProcessors设置为true,

// 该变量用于指示beanFactory是否已注册过InstantiationAwareBeanPostProcessors

this.hasInstantiationAwareBeanPostProcessors = true;

}

if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {

// 4.如果beanPostProcessor是DestructionAwareBeanPostProcessor, 则将hasInstantiationAwareBeanPostProcessors设置为true,

// 该变量用于指示beanFactory是否已注册过DestructionAwareBeanPostProcessor

this.hasDestructionAwareBeanPostProcessors = true;

}

}

该方法作用就是将 BeanPostProcessor 添加到 beanPostProcessors 缓存,这边的先移除再添加,主要是起一个排序的作用。而 hasInstantiationAwareBeanPostProcessors 和 hasDestructionAwareBeanPostProcessors 变量用于指示 beanFactory 是否已注册过 InstantiationAwareBeanPostProcessors 和 DestructionAwareBeanPostProcessor,在之后的 IoC 创建过程会用到这两个变量,这边先有个印象。

基本使用

====

使用方法比较简单,新建一个类实现 BeanPostProcessor 接口,并将该类注册到 Spring IoC 容器中。

package com.joonwhee.open.demo.spring;

import com.joonwhee.open.demo.service.UserService;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

import org.springframework.core.PriorityOrdered;

/**

  • @author joonwhee

  • @date 2019/2/23

*/

@Component

public class MyBeanPostProcessor implements BeanPostProcessor, PriorityOrdered {

@Override

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

System.out.println(“MyBeanPostProcessor#postProcessBeforeInitialization”);

if (bean instanceof UserService) {

System.out.println(beanName);

}

// 自己的逻辑

return bean;

}

@Override

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

System.out.println(“MyBeanPostProcessor#postProcessAfterInitialization”);

// 自己的逻辑

return bean;

}

@Override

public int getOrder() {

return 0;

结局:总结+分享

看完美团、字节、腾讯这三家的一二三面试问题,是不是感觉问的特别多,可能咱们真的又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。

开篇有提及我可是足足背下了Java互联网工程师面试1000题,多少还是有点用的呢,换汤不换药,不管面试官怎么问你,抓住本质即可!能读到此处的都是真爱

  • Java互联网工程师面试1000题

image.png

而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的 《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。

  • 程序员代码面试指南–IT名企算法与数据结构题目最优解

image.png

  • 其余像设计模式,建议可以看看下面这4份PDF(已经整理)

image.png

  • 更多的Java面试学习笔记如下,关于面试这一块,我额外细分出Java基础-中级-高级开发的面试+解析,以及调优笔记等等等。。。

image.png

以上所提及的全部Java面试学习的PDF及笔记,如若皆是你所需要的,那么都可发送给你!

加入社区:https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
码面试指南–IT名企算法与数据结构题目最优解**

[外链图片转存中…(img-5tI92yOy-1725731870877)]

  • 其余像设计模式,建议可以看看下面这4份PDF(已经整理)

[外链图片转存中…(img-WpfOJENf-1725731870878)]

  • 更多的Java面试学习笔记如下,关于面试这一块,我额外细分出Java基础-中级-高级开发的面试+解析,以及调优笔记等等等。。。

[外链图片转存中…(img-LG9Jnhs0-1725731870878)]

以上所提及的全部Java面试学习的PDF及笔记,如若皆是你所需要的,那么都可发送给你!

加入社区:https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值