美文网首页
001-BeanPostProcessor

001-BeanPostProcessor

作者: hangjun | 来源:发表于2018-10-25 15:56 被阅读0次

    1.目的

    以前也稍微了解spring的源码,最近在研究研究,记录一下心得。
    今天就针对spring中BeanPostProcessor的理解进行加深一下。

    2.BeanPostProcessor是什么?

    spring中基础对象都是bean! 那么beanPostProcessor也是一个bean,只不过多了一层关系,实现BeanPostProcessor接口。

    2.1.beanPostProcessor如何被识别出来的?

    在AbstractApplicationContext方法中registerBeanPostProcessors()
    代码行数:
    版本:spring-core-3.0.0.RELEASE-sources.jar
    方法中:AbstractApplicationContext.registerBeanPostProcessors() 401行

            registerBeanPostProcessors(beanFactory);
    
        protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
            String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
    

    2.2.beanPostProcessor中的方法何时被调用?

    在bean初始化的时候,也就是Spring激活bean的init-method方法的前后,会调用BeanPostProcessor的postProcessBeforeInitialization方法和postProcessAfterInitialization。
    代码行数:
    版本:spring-core-3.0.0.RELEASE-sources.jar
    方法中:AbstractAutowireCapableBeanFactory.initializeBean()
    1394
    1407

            
            Object wrappedBean = bean;
            if (mbd == null || !mbd.isSynthetic()) {
                wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
            }
    
            try {
                invokeInitMethods(beanName, wrappedBean, mbd);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(
                        (mbd != null ? mbd.getResourceDescription() : null),
                        beanName, "Invocation of init method failed", ex);
            }
    
            if (mbd == null || !mbd.isSynthetic()) {
                wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
            }
    

    3.分析一下BeanPostProcessor的实现类ApplicationContextAwareProcessor

    3.1.ApplicationContextAwareProcessor的作用?

    如果bean实现ApplicationContextAware的接口,那么就会将applicationContext注入到这个bean中。
    

    3.2.ApplicationContextAwareProcessor实现原理:

    1.添加到容器中:在AbstractApplicationContext的refresh方法中的prepareBeanFactory(beanFactory);方法。添加了ApplicationContextAwareProcessor处理器

         protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
         // Tell the internal bean factory to use the context's class loader etc.
         beanFactory.setBeanClassLoader(getClassLoader());
         beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
         beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
    
         // Configure the bean factory with context callbacks.
         beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
         beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
         beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
         beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
         beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
         beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    
         // BeanFactory interface not registered as resolvable type in a plain factory.
         // MessageSource registered (and found for autowiring) as a bean.
         beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
         beanFactory.registerResolvableDependency(ResourceLoader.class, this);
         beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
         beanFactory.registerResolvableDependency(ApplicationContext.class, this);
    
         .....
         ...
         ..
    }
    

    2.调用地方:ApplicationContextAwareProcessor是继承了BeanPostProcessor接口。在bean初始化的时候,也就是Spring激活bean的init-method方法的前后,会调用BeanPostProcessor的postProcessBeforeInitialization方法和postProcessAfterInitialization。

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

    3.3.ApplicationContextAware的作用。

    通过对ApplicationContextAwareProcessor的分析,我们可以清楚的了解ApplicationContextAware的作用:对当前bean传入对应的Spring上下文。(即得到applicationContext)

    3.4.ApplicationContextAware的使用实例。

    请点击以下的文章(自己看)
    请点击---文章1

    3.5.ApplicationContextAwareProcessor的使用实例

    请点击---[文章2](https://blog.csdn.net/qq_34310242/article/details/78266590)
    

    4.参考目录

    推荐:https://blog.csdn.net/mdq11111/article/details/79050280

    推荐:https://www.jianshu.com/p/2692bf784976

    https://www.jianshu.com/p/a90a3e617ba6

    相关文章

      网友评论

          本文标题:001-BeanPostProcessor

          本文链接:https://www.haomeiwen.com/subject/fhcctqtx.html