junjims 2017-08-22 18:54:18 浏览48 评论0
Java核心技术 源码 spring web 配置 class springmvc
摘要: 我们在使用SpringMVC的时候,做的第一件事情是配置**ContextLoaderListener**的监听器,这个监听器的作用,就是启动web容器的时候,自动装配ApplicationContext的配置信息,因为ContextLoaderListener实现了ServletContextListener接口,便会将ApplicationContext放置到servletContext中。
我们在使用SpringMVC的时候,做的第一件事情是配置**ContextLoaderListener**的监听器,这个监听器的作用,就是启动web容器的时候,自动装配ApplicationContext的配置信息,因为ContextLoaderListener实现了ServletContextListener接口,便会将ApplicationContext放置到servletContext中。
/**
* Initialize the root web application context
*/
@Override
public void contextInitialized(ServletContextEvent event) {
super.initWebApplicationContext(event.getServletContext());
}
/**
* Initialize Spring's web application context for the given servlet context,
*/
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
// 判断web.xml中存在多次ContextLoader定义
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException( "....");
}
try {
if (this.context == null) {
// 初始化context (重点)
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
if (cwac.getParent() == null) {
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
// 将webApplicationContext放入到ServletContext中
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
return this.context;
}
catch (RuntimeException ex) {
}
}
创建contextClass
这段代码就是创建webApplicationContext的核心,当前类的ContextLoader同样目录下一定会存在属性文件:ContextLoader.properties
**org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext**
然后通过反射的机制,进行实体类的创建
protected Class<?> determineContextClass(ServletContext servletContext) {
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
if (contextClassName != null) {
return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
} else {
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
}
}
整个源码跟踪下来,可以简单的归纳一下:
1. webApplicationContext存在性的验证
2. 创建webApplicationContext实例
3. 将实例记录在ServletContext中
4. 映射当前类加载器与创建的实例到全局变量中
private static final Map currentContextPerThread = new ConcurrentHashMap<>(1);)
版权声明:本文内容由互联网用户自发贡献,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至: yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
用云栖社区APP,舒服~
【云栖快讯】数据库技术天团集体亮相,分享一线生产实践经验,告诉你踩过的坑、走过的路,都是老司机,靠谱!干货分享,不可错过! 详情请点击