spring XmlWebApplicationContext详解
2011-09-13 22:32
459 查看
XmlWebApplicationContext是spring在web应用中的context,
在ContextLoaderListener或ContextLoaderServlet中完成了初步的设置。
XmlWebApplicationContext跟其他的applicationContext(FileSystem,Classpath)一样,是AbstractApplicationContext的一个子类。
applicationContext的refresh过程是线程同步的。
如下:
synchronized (this.startupShutdownMonitor)
方法1
prepareRefresh(); //设定起始时间,并设置active标记
方法2
// 通知子类完成BeanFactory的创建。
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
refreshBeanFactory 在AbstractRefreshableApplicationContext(是XmlWebApplicationContext的父类)中进行了实现。创建了DefaultListableBeanFactory,随后执行
loadBeanDefinitions(beanFactory);这个是在XmlWebApplicationContext中进行了实现。
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
}
该方法中的beanDefinitionReader完成了WEB-INF/applicationContext.xml(可指定contextConfigLoaction的值,可使用,; \t\n指定多个applicationContext.xml文件)的加载
// 上面浅黄色的方法如下
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);// 完成所有applicationContext.xml的加载
}
}
}
具体的xmlreader的装载再以后详细阅读。继续分析xmlwebApplicationContext的refresh过程。
方法3
prepareBeanFactory(beanFactory);
部分代码:
beanFactory.setBeanClassLoader(getClassLoader()); //设定类加载器
// 设定spring的EL解决器
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));
代码复杂,慢慢研究:
// 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();
文章出自:http://hi.baidu.com/yuanyuan1027/blog/item/9727c9dfd04d4a5995ee3723.html
在ContextLoaderListener或ContextLoaderServlet中完成了初步的设置。
XmlWebApplicationContext跟其他的applicationContext(FileSystem,Classpath)一样,是AbstractApplicationContext的一个子类。
applicationContext的refresh过程是线程同步的。
如下:
synchronized (this.startupShutdownMonitor)
方法1
prepareRefresh(); //设定起始时间,并设置active标记
方法2
// 通知子类完成BeanFactory的创建。
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
refreshBeanFactory 在AbstractRefreshableApplicationContext(是XmlWebApplicationContext的父类)中进行了实现。创建了DefaultListableBeanFactory,随后执行
loadBeanDefinitions(beanFactory);这个是在XmlWebApplicationContext中进行了实现。
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
}
该方法中的beanDefinitionReader完成了WEB-INF/applicationContext.xml(可指定contextConfigLoaction的值,可使用,; \t\n指定多个applicationContext.xml文件)的加载
// 上面浅黄色的方法如下
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);// 完成所有applicationContext.xml的加载
}
}
}
具体的xmlreader的装载再以后详细阅读。继续分析xmlwebApplicationContext的refresh过程。
方法3
prepareBeanFactory(beanFactory);
部分代码:
beanFactory.setBeanClassLoader(getClassLoader()); //设定类加载器
// 设定spring的EL解决器
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));
代码复杂,慢慢研究:
// 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();
文章出自:http://hi.baidu.com/yuanyuan1027/blog/item/9727c9dfd04d4a5995ee3723.html
相关文章推荐
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- spring XmlWebApplicationContext详解
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- 【Spring整合Struts2】Could not open ServletContext resource [/WEB-INF/application.xml]错误解决
- Spring源码学习之一 ContextLoaderListener及XmlWebApplicationContext
- spring applicationContext.xml 配置文件 详解
- web.xml & Spring applicationContext.xml简单配置(备忘)
- spring applicationContext.xml 配置文件 详解
- spring applicationContext.xml的配置文件详解
- spring web.xml applicationContext.xml常见配置
- Spring启动<二>——XmlWebApplicationContext
- 详解Spring的applicationContext.xml配置文件
- spring applicationContext.xml详解及模板
- Spring 配置文件applicationContext.xml详解
- javaWeb服务详解(含源代码,测试通过,注释) ——applicationContext.xml
- spring applicationContext.xml 配置文件 详解
- spring源码解析之默认配置文件名/WEB-INF/applicationContext.xml