Spring——扩展接口简介

参考 https://www.jianshu.com/p/2692bf784976

https://blog.csdn.net/asdfsadfasdfsa/article/details/71192476 简单用法

以下为应用

https://www.jianshu.com/p/3d099ea43b0e

https://www.jianshu.com/p/1417eefd2ab1

https://guoliangqi.iteye.com/blog/635826

https://www.cnblogs.com/wangpeng-friend/p/6247918.html

 主要功能调用时机接口实现类/子类及常见应用场景
BeanFactoryPostProcessor容器级别的扩展。

beanFactory完成了标准初始化流程,且所有definitions已被加载

,但是还没有开始实例化bean

PropertyPlaceholderConfigurer——用来替换配置文件的变量

CustomEditorConfigurer——解析一些特殊类型的属性,例如Date类型

BeanPostProcessorBean扩展

Bean实例化之后,会在初始化前后调用(即initializeBean方法中),有下面连个方法:

postProcessBeforeInitialization ——实例化前调用
postProcessAfterInitialization ——实例化后调用

 

XXXXXAware一般用来获取容器,例如获取BeanFactory, 获取AppplicationContext

BeanFactory是在bean初始化期间调用,

但ApplicationContext是在初始化之前调用。

因为ApplicationContext

是借助ApplicationContextAwareProcessor

的postProcessBeforeInitialization实现的

BeanFactoryAware

,ApplicationContextAware

ApplicationEventPublisherAware等等

InstantiationAwareBeanPostProcessor是一种特殊的BeanPostProcessor在bean实例化之前即调用(而不是初始化前后),返回代理bean

AOP原理就是通过实现

InstantiationAwareBeanPostProcesso

接口来返回代理对象的

    
    
    

 

以上扩展接口,多数都会在ApplicationContext启动期间注册,在getBean()时调用,

其中容器启动时调用了一个非常重要的方法叫refresh(),从中可以清楚看到大多数扩展接口的注册

public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                initMessageSource();

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

            catch (BeansException ex) {
                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset 'active' flag.
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }
        }
    }

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3300976/blog/3026870

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值