spring BeanPostProcessor接口

在上篇spring ApplicationListener接口(续)的例子中demo同时还实现了BeanPostProcessor接口

实现了这个接口,在spring 启动时,实现了这个接口的方法同样也会被调用,不同的是实现这个接口的类,每个bean初始化的时候都会被调用一次

先看一个这个接口的定义

/**
 * 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}.
 *
 * @author Juergen Hoeller
 * @since 10.10.2003
 * @see InstantiationAwareBeanPostProcessor
 * @see DestructionAwareBeanPostProcessor
 * @see ConfigurableBeanFactory#addBeanPostProcessor
 * @see BeanFactoryPostProcessor
 */
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.
     * @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
     */
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

    /**
     * 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
     * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
     * in contrast to all other BeanPostProcessor callbacks.
     * @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
     */
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}

这个接口就两个方法before和after,实现了这个接口则可以对方法进行包装拦截。但是最后一定要返回一个bean否则创建的bean就为空了

我就是最开始before方法直接返回空了,怎么搞控制台什么都没有

同样是上篇的例子输出如下

++++++++++++++++++++++postProcessAfterInitialization ++++++++++++++++++++++++++
dubbo-consumer
++++++++++++++++++++++postProcessAfterInitialization ++++++++++++++++++++++++++
com.alibaba.dubbo.config.RegistryConfig
++++++++++++++++++++++postProcessAfterInitialization ++++++++++++++++++++++++++
org.springframework.context.event.internalEventListenerProcessor
++++++++++++++++++++++postProcessAfterInitialization ++++++++++++++++++++++++++
org.springframework.context.event.internalEventListenerFactory
++++++++++++++++++++++postProcessAfterInitialization ++++++++++++++++++++++++++
dubbo
++++++++++++++++++++++postProcessAfterInitialization ++++++++++++++++++++++++++
myRealBean

原文地址:https://www.cnblogs.com/liguangming/p/10149243.html