BeanPostProcessor原理--使用讲解

《Spring源码解析》笔记

BeanPostProcessor原理学习

在学习BeanPostProcessor的原理学习完之后,对Spring如何使用充满好奇,尝试使用例子进行理解,以下记录过程:

1、使用ApplicationContextAware,可以指定,在当前函数中获取到容器上下文,具体使用举例如下:

@Component
public class Dog implements ApplicationContextAware {

    //@Autowired
    private ApplicationContext context;

    public Dog(){
        System.out.println("dog constructor...");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // TODO Auto-generated method stub
        this.context = applicationContext; } }

2、在Dog类中通过使用变量context即可获取Spring的容器上下文,但是具体如何实现?

     ApplicationContextAware本身并没有继承BeanPostProcess,只继承Aware接口 

public interface ApplicationContextAware extends Aware {

    /**
     * Set the ApplicationContext that this object runs in.
     * Normally this call will be used to initialize the object.
     * <p>Invoked after population of normal bean properties but before an init callback such
     * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
     * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
     * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
     * {@link MessageSourceAware}, if applicable.
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws ApplicationContextException in case of context initialization errors
     * @throws BeansException if thrown by application context methods
     * @see org.springframework.beans.factory.BeanInitializationException
     */
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

  通过代码运行,可以看到调用,该setApplicationContext是通过类ApplicationContextAwareProcessor实现,

  查看ApplicationContextAwareProcessor:

class ApplicationContextAwareProcessor implements BeanPostProcessor {}

当容器中的Bean赋值后,会遍历容器中的BeanPostProcessor ,挨个执行,自然会执行到ApplicationContextAwareProcessor 的postProcessBeforeInitialization()方法。

在其中执行的是this.invokeAwareInterfaces(bean)函数,查看invokeAwareInterfaces()函数代码:

private void invokeAwareInterfaces(Object bean) {
        if (bean instanceof Aware) {
            if (bean instanceof EnvironmentAware) {
                ((EnvironmentAware)bean).setEnvironment(this.applicationContext.getEnvironment());
            }

            if (bean instanceof EmbeddedValueResolverAware) {
                ((EmbeddedValueResolverAware)bean).setEmbeddedValueResolver(this.embeddedValueResolver);
            }

            if (bean instanceof ResourceLoaderAware) {
                ((ResourceLoaderAware)bean).setResourceLoader(this.applicationContext);
            }

            if (bean instanceof ApplicationEventPublisherAware) {
                ((ApplicationEventPublisherAware)bean).setApplicationEventPublisher(this.applicationContext);
            }

            if (bean instanceof MessageSourceAware) {
                ((MessageSourceAware)bean).setMessageSource(this.applicationContext);
            }

            if (bean instanceof ApplicationContextAware) {
                ((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
            }
        }

    }

3、从代码中可以看出

     该函数只会执行实现Aware接口的Bean,我们的实现类Dog实现的是ApplicationContextAware

    即会执行其相关的代码,自动为Dog中变量进行赋值。

上一篇:Spring Bean生命周期


下一篇:生命周期-BeanPostProcessor-后置处理器