《系列二》-- 7、后置处理器-PostProcessor

阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需要看官自己去源码中验证。全系列文章基于 spring 源码 5.x 版本。

写在开始前的话:

阅读spring 源码实在是一件庞大的工作,不说全部内容,单就最基本核心部分包含的东西就需要很长时间去消化了:

  • beans
  • core
  • context

实际上我在博客里贴出来的还只是一部分内容,更多的内容,我放在了个人,fork自 spring 官方源码仓了; 而且对源码的学习,必须是要跟着实际代码层层递进的,不然只是干巴巴的文字味同嚼蜡。

https://gitee.com/bokerr/spring-framework-5.0.x-study

这个仓设置的公共仓,可以直接拉取。



Spring源码阅读系列--全局目录.md



什么是后置处理器

这里引入spring 的一个 重要部件 PostProcessor, 我习惯把它叫做:后置处理器;

通俗来说,就是:定义一个 XxxPostProcessor 接口,定义一组行为;而后在具体的bean加载过程中,我们可以在 BeanFactory 初始化时,根据自己的实际需要,向BeanFactory 中注入相关的 PostProcessor;
而后在某些特殊节点(时机),获取某一类的 "后置处理器" 并全部执行之即可。

这里需要注意的是:后置处理器,一般需要被理解成,某某行为-后置处理器;它可能是对某某行为返回结果的校验,也可能是对其的包装,(希望你了解设计模式之:包装器模式)。

spring 源码中已知的,顶级PostProcessor

img.png

全局一共6个,那么我们通过,其中跟我们当前分析的代码关联度最高的, BeanPostProssor 来做个介绍:


package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;

/**
 * Factory hook that allows for custom modification of new bean instances,
 * e.g. checking for marker interfaces or wrapping them with proxies.
 *
 * <p>ApplicationContexts can autodetect BeanPostProcessor beans in their
 * bean definitions and apply them to any beans subsequently created.
 * Plain bean factories allow for programmatic registration of post-processors,
 * applying to all beans created through this factory.
 *
 * <p>Typically, post-processors that populate beans via marker interfaces
 * or the like will implement {@link #postProcessBeforeInitialization},
 * while post-processors that wrap beans with proxies will normally
 * implement {@link #postProcessAfterInitialization}.
 */
public interface BeanPostProcessor {

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 */
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
	 * instance and the objects created by the FactoryBean (as of Spring 2.0). The
	 * post-processor can decide whether to apply to either the FactoryBean or created
	 * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
	 * <p>This callback will also be invoked after a short-circuiting triggered by a
	 * in contrast to all other BeanPostProcessor callbacks.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 */
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}
}

可以看接口中的这俩方法名:

  • postProcessBeforeInitialization: 后置处理器 of 初始化前
    所有bean在"初始化前"都会执行,实现了接口BeanPostProcessor 的后置处理器,所实现的 postProcessBeforeInitialization 方法。
    该方法返回的可能是该bean的实例,也可能是被代理包装后的该bean

  • postProcessAfterInitialization: 后置处理器 of 初始化后
    所有bean在 "初始化后" 都会执行,实现了接口BeanPostProcessor 的后置处理器,所实现的 postProcessBeforeInitialization 方法。
    该方法返回的可能是该bean的实例,也可能是被代理包装后的该bean

我这里的翻译较为缩略,英文尚可的伙计可以自取源码中的注释。

其它 "后置处理器"

其实大同小异,其它的就不再展开介绍。

posted @ 2023-06-24 10:05  bokerr  阅读(84)  评论(0编辑  收藏  举报