BeanFactoryPostProcessor与BeanPostProcessor区别

BeanFactoryPostProcessor

package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;

/**
 * Allows for custom modification of an application context's bean definitions,
 * adapting the bean property values of the context's underlying bean factory.
 * 允许自定义修改application context的BeanDefinition,注意:是修改bean定义(beandefinition),不是bean实例,此时bean还没有实例化
 * 调整application context的底层Bean工厂的中的Bean属性值。
 * 这意味着BeanFactoryPostProcessor是在bean new之前用的
 *
 * <p>Application contexts can auto-detect BeanFactoryPostProcessor beans in
 * their bean definitions and apply them before any other beans get created.
 *
 * <p>Useful for custom config files targeted at system administrators that
 * override bean properties configured in the application context.
 *
 * <p>See PropertyResourceConfigurer and its concrete implementations
 * for out-of-the-box solutions that address such configuration needs.
 *
 * <p>A BeanFactoryPostProcessor may interact with and modify bean
 * definitions, but never bean instances. Doing so may cause premature bean
 * instantiation, violating the container and causing unintended side-effects.
 * If bean instance interaction is required, consider implementing
 * {@link BeanPostProcessor} instead.
 *
 */
 // 这个类里面的所有方法,都是在bean完成实例化之前使用的,主要就是修改bean的元数据的,通俗点说,这里面的方法都是调用构造函数之前使用的,这样修改后的元数据才能生效
@FunctionalInterface
public interface BeanFactoryPostProcessor {

	/**
	 * Modify the application context's internal bean factory after its standard
	 * initialization. All bean definitions will have been loaded, but no beans
	 * will have been instantiated yet. This allows for overriding or adding
	 * properties even to eager-initializing beans.
	 * 所有bean定义都将被加载,但是还没有实例化bean 
	 * 允许覆盖属性值,或添加新的属性
	 
	 * @param beanFactory the bean factory used by the application context
	 * @throws org.springframework.beans.BeansException in case of errors
	 */
	void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

BeanPostProcessor

package org.springframework.beans.factory.config;

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

/**
 * 首先介绍什么是hook,hook汉语意为钩子,在java中意思是表示在事件到达终点前进行拦截或监控的一种行为,一个词:拦截

 * Factory hook that allows for custom modification of new bean instances,
 * e.g. checking for marker interfaces or wrapping them with proxies.
 * 工厂钩子,允许自定义修改 新的bean实例,注意:是修改bean实例,不是bean定义(beanDefinition)
 * 例如检查标记接口或使用代理包装它们。
 *
 * <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.
 * ApplicationContexts在它的所有的BeanDefinitions中自动探测 BeanPostProcessor beans,
 * 并且对随后创建的Bean使用BeanPostProcessor。
 * 这意味着BeanPostProcessor是在bean 实例化之后用的
 *
 * <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}.
 */
 // 这个类里面的所有方法,都是在bean完成实例化之后使用的,通俗点说,这里面的方法都是调用完构造函数、以及set完 成员变量之后使用的,
 // 区别在于构造函数之后,还有一些回调方法,比如属性完成装配的回调,以及bean的init-method方法,这个BeanPostProcessor类切入的正是回调方法前后的地方
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.
	 * 在Bean的 实例化回调方法 之前(比如InitializingBean#afterPropertiesSet方法、自定义的init-method方法),对给定的bean instance使用BeanPostProcessor,
	 * 注意是在 实例化回调方法(就是实例化完成后调用的方法) 之前使用,不是在 实例化方法 之前使用
	 * bean实例已然已经设置好属性了,也就是说在bean使用BeanPostProcessor之前,bean已经完成了属性的装配
	 * 返回值可能是对原始实例包装后的包装类
	 
	 * @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;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 */
	//为在Bean的初始化回调方法前提供回调入口
	@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.
	 * 在Bean的 实例化回调方法 之后(比如InitializingBean#afterPropertiesSet方法、自定义的init-method方法),对给定的bean instance使用BeanPostProcessor,
	  
	 * <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
	 * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
	 * 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;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 * @see org.springframework.beans.factory.FactoryBean
	 */
	//为在Bean的初始化回调方法后提供回调入口
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

总结

1、BeanFactoryPostProcessor在bean new之前使用,用于修改bean的元数据
2、BeanPostProcessor在bean new之后使用,用于修改 bean的实例

总体流程

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值