spring BeanProcessor接口详解
作者:天行者1006 发布时间:2021-12-26 07:32:17
目录
1. 简单认识BeanProcessor
BeanProcessor的理解
一个BeanProcessor的使用实例
2. BeanProcessor的实现思路和简化实例
BeanProcessor大概的实现思路
一个BeanProcessor的简化逻辑实例
net.postProcessor.PostProcessor.java
3. spring中BeanProcessor的源码解析
1. 简单认识BeanProcessor
BeanProcessor的理解
BeanProcessor是spring中的一个重要接口,他有两个接口方法一个是postProcessBeforeInitialization前置初始化,另一个是postProcessAfterInitialization后置初始化。从名称上就可以大概清楚这个接口的作用:在一个业务流程的前后加入两个接口方法,当执行这个业务流程时,就会触发这两个接口方法的执行。简单的总结一下有两个要点:
在业务流程中,根据BeanProcessor接口方法加在不同的位置(一般是前后),可以实现对业务逻辑的扩展。
在业务逻辑执行前,BeanProcessor的实现类必须已经被创建完成(BeanProcessor接口类必须要优先实例化)。
而在spring中,就有很多实现了BeanProcessor的bean,通过在重要的业务流程(如bean的生命周期流程)的前后加上BeanProcessor接口方法,就可以对业务逻辑进行修改或补充。
一个BeanProcessor的使用实例
在spring的bean生命周期中,BeanProcessor接口方法会在bean创建后的初始化方法(init-method或@PostConstruct指向的方法)前后执行before和after方法;那有没有在bean创建前后执行的接口方法呢?答案是肯定有的,这个功能是由BeanProcessor的子接口InstantiationAwareBeanPostProcessor来实现的,他也是有before和after方法,会在bean实例化前后执行。
我们先定义一个BeanProcessor接口实现类和一个InstantiationAwareBeanPostProcessor接口实现类。
BeanPostProcessor实现类:
//net.postProcessor.CustomerPostProcessor
@Component
public class CustomerPostProcessor implements BeanPostProcessor {
@PostConstruct
public void init(){
System.out.println("执行CustomerPostProcessor的PostConstruct");
}
public CustomerPostProcessor(){
System.out.println("执行CustomerPostProcessor的构造方法");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======BeforeInitialization======"+ beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======AfterInitialization======"+ beanName);
return bean;
}
}
InstantiationAwareBeanPostProcessor实现类:
//net.postProcessor.CustomerInitialPostProcessor
@Component
public class CustomerInitialPostProcessor implements InstantiationAwareBeanPostProcessor {
@PostConstruct
public void init(){
System.out.println("执行CustomerInitialPostProcessor的PostConstruct");
}
public CustomerInitialPostProcessor(){
System.out.println("执行CustomerInitialPostProcessor的构造方法");
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("bean初始化前执行:class为"+beanClass.getName()+"|beanName为"+beanName);
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("bean初始化后执行:Object为"+bean+"|beanName为"+beanName);
return false;
}
}
再创建一个普通的bean对象:
//net.postProcessor.FirstBean
@Component
public class FirstBean implements InitializingBean {
private String msg = "hello";
@PostConstruct
public void init(){
System.out.println("执行FirstBean的PostConstruct");
}
public FirstBean(){
System.out.println("FirstBean构造方法!"+msg);
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("执行FirstBean的afterPropertiesSet");
}
}
我们创建一个spring工厂对象将上述bean加载进去:
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("net.postProcessor");
}
//执行得到以下结果:
执行CustomerInitialPostProcessor的构造方法
执行CustomerInitialPostProcessor的PostConstruct
执行CustomerPostProcessor的构造方法
执行CustomerPostProcessor的PostConstruct
bean初始化前执行:class为net.postProcessor.FirstBean|beanName为firstBean
FirstBean构造方法!hello
bean初始化后执行:Object为net.postProcessor.FirstBean@79179359|beanName为firstBean
net.postProcessor.FirstBean@79179359======BeforeInitialization======firstBean
执行FirstBean的PostConstruct
执行FirstBean的afterPropertiesSet
net.postProcessor.FirstBean@79179359======AfterInitialization======firstBean
通过上述结果证明了我们之前的说法是正确的:
1.BeanPostProcessor接口类会优先实例化,且在实例化中无法不会调用BeanPostProcessor接口方法的
2.InstantiationAwareBeanPostProcessor接口方法会在FirstBean构造方法构造方法前后执行
3.BeanPostProcessor接口方法会在FirstBean实例化后进行初始化的前后执行
注意:若@PostConstruct注解方法方法未执行,请加入javax.annotation:javax.annotation-api:1.3.2jar包依赖,原因是@PostConstruct是J2EE标准的注解,不是spring自己的接口,而在JDK8往上的版本中设计者打算弃用这些注解,所以做了处理,我们是没有办法直接使用J2EE标准注解的(@Resource、@PostConstruct、@PreDestroy等几个注解),为了兼容这种情况,所以有了javax.annotation-apijar包的产生(或者降低JDK版本)。
2. BeanProcessor的实现思路和简化实例
BeanProcessor大概的实现思路
通过之前的了解BeanProcessor的使用,我们可以知道BeanProcessor并不复杂,但是却十分的重要,下面来分析下BeanProcessor的实现思路:
创建个接口A,接口包含一些切点方法(Before、After、Around之类的),实现这个接口A的类要在使用前就创建好
我们需要有个业务流程,这个业务流程由若干步组成;将接口A的接口方法插入到这些业务步骤之间(需要扩展的地方)
要执行这个业务流程时,把接口A的实现类对象赋值到业务流程中,在执行业务流程中,就会触发接口方法的执行完成功能扩展
当我们更换赋值到业务流程中的接口A的实现类时,对应的扩展逻辑也会随之变化,这样就实现了可插拔式的扩展逻辑(策略模式)。
一个BeanProcessor的简化逻辑实例
在spring中我们可以创建任意数量的bean实现BeanProcessor接口,所以实际上我们是要一个全局的beanProcessorList对象用来存储这些BeanProcessor对象;在执行业务代码时,要循环这个beanProcessorList对象,获取你需要的BeanProcessor对象来执行接口方法。下面是一个模拟spring bean生命周期的简化版,来帮助你理解spring中BeanProcessor的工作原理。
net.postProcessor.SecondBean.java
@Component
public class SecondBean {
private String msg = "world";
public SecondBean(){
System.out.println("SecondBean构造方法!"+msg);
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
net.postProcessor.CustomerPostProcessor.java
@Component
public class CustomerPostProcessor implements BeanPostProcessor {
@PostConstruct
public void init(){
System.out.println("执行CustomerPostProcessor的PostConstruct");
}
public CustomerPostProcessor(){
System.out.println("执行CustomerPostProcessor的构造方法");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======BeforeInitialization======"+ beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======AfterInitialization======"+ beanName);
return bean;
}
}
net.postProcessor.PostProcessor.java
public class PostProcessor {
//模拟扫描到的bean信息<"SecondBean", "net.postProcessor.SecondBean">
Map<String, String> scanBeanMap = new HashMap<>();
//模拟spring的beanPostProcessors列表
List<BeanPostProcessor> processorBeanList = new ArrayList<>();
//模拟bean对象缓存
Map<String, Object> beanCache = new HashMap<>();
//添加扫描的bean信息
public PostProcessor addBeanInfo(String beanName, String classPath){
this.scanBeanMap.put(beanName, classPath);
return this;
}
//模拟bean创建流程
public Object execute(){
try {
//先临时存储实现了postProcessor接口的bean对象
List<BeanPostProcessor> postProcessorStrList = new ArrayList<>();
//循环scanBeanMap,获取bean列表中实现了postProcessor接口的类,加入processorBeanList中
for(String temp: scanBeanMap.keySet()){
Class<?> clazz = Class.forName(scanBeanMap.get(temp));
//判断是否实现了BeanPostProcessor接口
if(BeanPostProcessor.class.isAssignableFrom(clazz)){
//实例化让如临时容器
postProcessorStrList.add((BeanPostProcessor)createBean(temp));
}
}
//将实现了postProcessor接口的bean加入processorBeanList中
for(BeanPostProcessor obj: postProcessorStrList){
processorBeanList.add(obj);
}
//再次循环scanBeanMap初始化所用bean
for(String temp: scanBeanMap.keySet()){
createBean(temp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
//bean实例化
public Object createBean(String beanName){
//从缓存中获取
if(beanCache.containsKey(beanName)){
return beanCache.get(beanName);
}else{
//缓存中取不到,则进行创建后加入缓存
try {
Class<?> clazz = Class.forName(scanBeanMap.get(beanName));
//processor前置方法执行
for(BeanPostProcessor processor : processorBeanList){
processor.postProcessBeforeInitialization(clazz, beanName);
}
//bean实例化
Object result = clazz.getConstructor().newInstance();
//processor后置方法执行
for(BeanPostProcessor processor : processorBeanList){
processor.postProcessAfterInitialization(result, beanName);
}
//将bean加入缓存
beanCache.put(beanName, result);
return result;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e){
e.printStackTrace();
}
}
return null;
}
}
代码调用
public static void main(String[] args) {
PostProcessor postProcessor = new PostProcessor();
//添加扫描到的bean
postProcessor
.addBeanInfo("SecondBean", "net.postProcessor.SecondBean")
.addBeanInfo("CustomerPostProcessor", "net.postProcessor.CustomerPostProcessor");
postProcessor.execute();
}
//执行结果
执行CustomerPostProcessor的构造方法
class net.postProcessor.SecondBean======BeforeInitialization======SecondBean
SecondBean构造方法!world
net.postProcessor.SecondBean@1b40d5f0======AfterInitialization======SecondBean
代码逻辑如下:
循环bean信息列表,将BeanPostProcessor接口bean分离出来优先实例化(实例化中缓存bean对象),并将之放入临时容器。
循环完成,将临时容器中的BeanPostProcessor接口bean赋值到全局BeanPostProcessor接口列表中
再次循环bean信息列表,缓存存在则直接返回缓存对象,不存在则进行bean实例化,期间循环调用全局BeanPostProcessor接口对象方法
3. spring中BeanProcessor的源码解析
我们要从spring中的refresh()开始看起:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
//刷新准备
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
//告诉子类刷新内部bean工厂。
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
//为容器准备bean工程
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
//允许在上下文bean的后处理工厂子类。
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
//优先将BeanDefinitionRegistryPostProcessor\BeanFactoryPostProcessor接口的bean对象实例化
//属于spring内部组件调用
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
//处理用户自定义PostProcessor接口对象,之后加入spring的beanPostProcessors列表,
// 供之后预实例化其他bean时触发这些PostProcessor方法
registerBeanPostProcessors(beanFactory);
//...省略代码
//实例化所有(non-lazy-init)单件。
finishBeanFactoryInitialization(beanFactory);
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
//bean销毁
destroyBeans();
// Reset 'active' flag.
//取消刷新
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
//重置公共缓存
resetCommonCaches();
}
}
}
其中包含有postProcess字段都有可能和BeanProcessor相关,这里有三个相关方法:
postProcessBeanFactory(beanFactory),这个是一共空的扩展方法,显然无关
invokeBeanFactoryPostProcessors(beanFactory),处理spring中实现了BeanProcessor接口的内部组件直接调用接口方法
registerBeanPostProcessors(beanFactory),实例化用户自定义BeanProcessor接口bean组件,之后循环赋值到全局BeanProcessor列表中
所以registerBeanPostProcessors()就是我们要找的对象,来跟进看下registerBeanPostProcessors():
//AbstractApplicationContext#registerBeanPostProcessors
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//委托给PostProcessorRegistrationDelegate.registerBeanPostProcessors进行处理
PostProcessorRegistrationDelegate.registerBeanPostProcessors进行处理(beanFactory, this);
}
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
//查询实现了BeanPostProcessor接口的beanName
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 BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
//根据beanName循环调用getBean进行实例化
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);
}
}
// First, register the BeanPostProcessors that implement PriorityOrdered.
//对BeanPostProcessor接口对象进行排序
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
//将获取到的PostProcessors接口对象加入到spring的beanPostProcessors列表
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered.
List<BeanPostProcessor> 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<BeanPostProcessor> 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.
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));
}
果然这里就是处理BeanPostProcessor接口的地方,逻辑和之前的思路类似:
循环扫描到的bean列表,获取实现了BeanPostProcessor接口的beanName数组
循环beanName数组数组,调用beanFactory.getBean()将bean实例化,并放入priorityOrderedPostProcessors列表中
调用sortPostProcessors对priorityOrderedPostProcessors列表进行排序(处理BeanPostProcessor调用的顺序)
调用registerBeanPostProcessors将priorityOrderedPostProcessors列表中的bean对象赋值到全局列表beanPostProcessors中
回到refresh()中,当调用finishBeanFactoryInitialization()对所用bean进行预实例化时就会调用这些BeanPostProcessor接口方法
来源:https://juejin.cn/post/6844904196026843149


猜你喜欢
- onCreate( )方法是android应用程序中最常见的方法之一,那么,我们在使用onCreate()方法的时候应该注意哪些问题呢?&n
- java.util.Scanner类是一个简单的文本扫描类,它可以解析基本数据类型和字符串。它本质上是使用正则表达式去读取不同的数据类型。J
- 目录多通道分离API操作通道合并API操作结果源码多通道分离APIpublic static void split(Mat m, List&
- Android手机或平板都会存在横竖屏切换的功能,通常是由物理重力感应触发的,但是有时候也不尽然,通常在设置里面我们可以对手机的横竖屏切换进
- 一、Thread 的常见构造方法方法说明Thread()创建线程对象Thread(Runnable target)使用 Runnable 对
- 问题: 在win10系统中,使用IDEA 查看源码的时候,经常跳转代码,这样会让使用者感觉和方便,但是当你想要返回上次跳转的位置时,却不知道
- 本文实例为大家分享了Android来电拦截的方法,供大家参考,具体内容如下权限 <uses-permission andr
- 本文实例讲述了Android编程实现分页加载ListView功能。分享给大家供大家参考,具体如下:package eoe.listview;
- 本文实例为大家分享了Android画笔屏幕锁小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下1.如果使用GestureOverlay
- 前言WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在 WebSocket API 中,浏
- 介绍今天主要分享一下 kafka 的 rebalance,在 kafka 中,rebalance 是一个十分重要的概念,很多时候引发的一些问
- 服务端在平台上创建springboot小程序应用创建小程序登录蚂蚁金服开放平台,扫码登录填写信息后,点击支付宝小程序,选择立即接入 >
- 在这里,我们将用到finish(),简单介绍一下它的使用:finish()官方解析:Call this when your activity
- 假设你已经装了texlive打开cmd输入latex --version应该能输出打开vscode,安装这几个插件设置->Settin
- 这篇文章主要介绍了Jenkins Host key verification failed问题解决,文中通过示例代码介绍的非常详细,对大家的
- 前言本文基于itext7实现pdf加水印和合并的操作。实际上在我们实际项目应用中,对于pdf的操作也是比较常见的,我上一个项目中就有将结果转
- 本人所使用的开发环境是VS2008,开发的系统所在移动终端版本为windows mobile 5.0。由于需要进行身份的验证,需要获取移动终
- 前言大家应该都遇到过,在工作和生活中经常要填写一些个人资料,这时候往往需要放证件照上去,但是有时候人家要求是红底或白底,但是偏偏不巧的是你以
- 本实例将显示类似于windows7提供的图片预览窗格效果,单击任意一张图片,可以在右侧显示该图片的预览效果。效果如图所示:具体实现方法:re
- 前言Apache POI [1] 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Mi