【知识库】--spring ApplicationContext 加载配置文件 refresh()(241)
来源:互联网 发布:淘宝设置不包邮地区 编辑:程序博客网 时间:2024/06/13 17:26
ApplicationContext 扩展了 BeanFactory,大部分的企业应用使用ApplicationContext。
加载配置文件:
BeanFactory加载XML:BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactory.xml"));
ApplicationContext bf = new ClassPathXmlApplicationContext("beanFactory.xml");
public ClassPathXmlApplicationContext(String configLocation) throws BeansException{
this.(new String[] {configLocation} , true ,null);
}
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException{
super(parent);
setConfigLocations(configLocations);
if(refresh){ refresh();}
}
1 设置配置路径:
public void setConfigLocations(String[] locations){
if(locations != null) {
Assert.noNullElements(locations,"Config locations must be null");//数组中不可有null
this.configLoactions = new String[location.length];
for(int i = 0; i < locations.length; I++){
this.configLocations[i] = resolvePath(locations[i]).trim(); //解析给定的路径
}
}else{
this.configLocations = null;
}
}
2 扩展功能--refresh()
public void refresh() throws BeansException, IllegalStateException{
synchronized (this.startupShutdownMonitor){
//准备刷新的上下文环境 参考
prepareRefresh();
//tell the subclass to refresh the internal bean factory 初始化beanFactory,进行XML文件读取
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); 参考
//prepare the bean factory for use in this context 对BeanFactory 进行功能填充 @Qualifier @Autowired 注解在此增强
prepareBeanFactory(beanFactory);
try{
//allows post-processing of the bean factory in context subclass 子类覆盖方法做额外的处理。可以在此进行业务相关的进一步扩展。
postProcessBeanFactory(beanFactory);
//激活各种BeanFactory处理器
invokeBeanFactoryPostProcessors(beanFactory);
//注册拦截Bean创建的Bean处理器,此处只是注册,真正的调用是在getBean时候
registerBeanPostProcessors(beanFactory);
//为上下文初始化Message源,国际化处理
initMessageSource();
//initialize event multicaster for this context 初始化应用消息广播器
initApplicationEventMulticaster();
//initialize other special beans in specific context subclasses 留给子类初始化其他的Bean
onRefresh();
//check for listener beans and register them 在所有注册的bean中查找Listener Bean ,注册到消息广播中
registerListeners();
// instantiate all remaining (non-lazy-init) singletons 初始化剩下的单实例
finishBeanFactoryInitialization(beanFactory);
//最后一步 publish corresponding event
//完成刷新过程,通知生命周期处理器 lifecycleProcessor 刷新完成,同时发出ContextRefreshEvent 通知别人
finishRefresh();
}catch(BeanException ex){ destroyBeans(); cancelRefresh(ex); throw ex;}
}
}
- 【知识库】--spring ApplicationContext 加载配置文件 refresh()(241)
- 【知识库】--spring ApplicationContext 环境准备 prepareRefresh()(241)
- 【知识库】--spring ApplicationContext 获取BeanFactory obtainFreshBeanFactory()(241)
- 【知识库】--spring ApplicationContext 扩展起始点-prepareBeanFactory-registerResolvableDependency(241)
- Spring 加载xml配置文件的方式 ApplicationContext
- Spring加载配置文件applicationContext.xml的方式
- 【知识库】--spring ApplicationContext 功能的扩展起始点-prepareBeanFactory(241)
- spring学习笔记(3)——配置文件applicationContext.xml的加载
- Spring学习-03:ApplicationContext加载配置文件的两种方式
- spring配置文件详解(applicationContext.xml详解)
- spring配置文件详解(applicationContext.xml详解)
- spring配置文件详解(applicationContext.xml详解)
- Spring配置文件(applicationContext.xml如何配置)
- spring配置文件详解(applicationContext.xml)
- spring配置文件applicationContext说明
- spring配置文件applicationContext.xml
- spring配置文件 applicationContext.xml
- Spring配置文件-applicationContext.xml
- Android studio 依赖冲突的解决
- python 列表字符串字典
- 解决Eclipse Debug 的source not found问题
- Python利用字典实现switch功能
- Django-路由系统
- 【知识库】--spring ApplicationContext 加载配置文件 refresh()(241)
- Head First设计模式--第一章-策略模式
- 【Tensorflow】怎样为你的网络预加工和打包训练数据?(二):小数据集的处理方案
- redis之key、string、list、set、sort set、hash、精细持久化、snapshotting快照保存、主从模式
- Android学习之Service(上)
- Python日期与字符串互转
- Struts2笔记01 Struts2中filter有无ng
- ZIP压缩的原理
- 读书笔记-人月神话5