参考 Spring IoC (二)实例化、初始化
BeanPostProcessor是使用IoC容器时经常会遇到的一个特性,这个Bean的后置处理器是一个监听器,它
可以监听容器触发的事件。将它向IoC容器注册后,容器中管理的Bean具备了接受IoC容器事件回调的能力。
BeanPostProcessor接口
1 public interface BeanPostProcessor { 2 3 /** 4 * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean 5 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} 6 * or a custom init-method). The bean will already be populated with property values. 7 * The returned bean instance may be a wrapper around the original. 8 * @param bean the new bean instance 9 * @param beanName the name of the bean 10 * @return the bean instance to use, either the original or a wrapped one; if 11 * {@code null}, no subsequent BeanPostProcessors will be invoked 12 * @throws org.springframework.beans.BeansException in case of errors 13 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet 14 */ 15 Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; 16 17 /** 18 * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean 19 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} 20 * or a custom init-method). The bean will already be populated with property values. 21 * The returned bean instance may be a wrapper around the original. 22 * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean 23 * instance and the objects created by the FactoryBean (as of Spring 2.0). The 24 * post-processor can decide whether to apply to either the FactoryBean or created 25 * objects or both through corresponding {@code bean instanceof FactoryBean} checks. 26 * <p>This callback will also be invoked after a short-circuiting triggered by a 27 * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method, 28 * in contrast to all other BeanPostProcessor callbacks. 29 * @param bean the new bean instance 30 * @param beanName the name of the bean 31 * @return the bean instance to use, either the original or a wrapped one; if 32 * {@code null}, no subsequent BeanPostProcessors will be invoked 33 * @throws org.springframework.beans.BeansException in case of errors 34 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet 35 * @see org.springframework.beans.factory.FactoryBean 36 */ 37 Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; 38 39 }
流程图
通过populateBean方法和 initializeBean调用