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

发布时间 2023-06-24 10:12:42作者: bokerr

什么是后置处理器

这里引入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

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

其它 "后置处理器"

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