欢迎来到天天文库
浏览记录
ID:37898076
大小:58.00 KB
页数:9页
时间:2019-06-02
《Spring源码解析-applicationContext》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Spring源码解析-applicationContextDemouml类图ApplicationContextApplicationListener源码解析主流程obtainFreshBeanFactoryprepareBeanFactoryinvokeBeanFactoryPostProcessorsregisterBeanPostProcessorsregisterListenersfinishRefresh总结在已经有BeanFactory可以完成Ioc功能情况下,spring又提供了ApplicationContext,why
2、?可以简单理解BeanFactory为可用,ApplicationContext封装了Beanfactory,提供了上下文、国际化、监听等更多的可选功能,让其能用,使用时,优先使用ApplicationContext。Demo跟beanfactory的demo唯一不同的就是://BeanFactoryctx=newXmlBeanFactory(newClassPathResource("spring.xml"));ApplicationContextctx=newClassPathXmlApplicationContext("sprin
3、g.xml");uml类图ApplicationContextapplicationContextApplicationListenerApplicationListener源码解析主流程//ClassPathXmlApplicationContextpublicClassPathXmlApplicationContext(String[]configLocations,booleanrefresh)throwsBeansException{this(configLocations,refresh,null);}publicClassP
4、athXmlApplicationContext(String[]configLocations,booleanrefresh,ApplicationContextparent)throwsBeansException{super(parent);//设置configLocationssetConfigLocations(configLocations);if(refresh){refresh();}}//AbstractApplicationContextpublicvoidrefresh()throwsBeansException,
5、IllegalStateException{synchronized(this.startupShutdownMonitor){//设置和校验系统变量和环境变量的值prepareRefresh();//获取beanFactoryConfigurableListableBeanFactorybeanFactory=obtainFreshBeanFactory();//配置BeanFactory上下文prepareBeanFactory(beanFactory);try{//子类扩展,beanFactory的后置处理postProcessB
6、eanFactory(beanFactory);//调用BeanFactory的后置处理器invokeBeanFactoryPostProcessors(beanFactory);//注册beanprocessors,只是注册,bean创建时调用.registerBeanPostProcessors(beanFactory);//国际化initMessageSource();//初始化监听的Multicaster,有自定义就是用自定义,没有就使用默认SimpleApplicationEventMulticasterinitApplica
7、tionEventMulticaster();//子类扩展onRefresh();//注册listenregisterListeners();//实例化所有非延迟加载的bean,相当于循环调用getbeanfinishBeanFactoryInitialization(beanFactory);//完成刷新,通知LifecycleProcessor,ContextRefreshedEvent事件(这个很有用,可以监听这个事件)finishRefresh();}catch(BeansExceptionex){//Destroyalread
8、ycreatedsingletonstoavoiddanglingresources.destroyBeans();//Reset'active'flag.cancelRefresh(ex);//Propa
此文档下载收益归作者所有