# spring 源码学习笔记 未编辑完成
2017-03-24 19:31
267 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_23020607/article/details/65638894
web.xml 配置spring
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-ontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
ContextLoaderListener继承ContextLoader,并实现ServletContextListener
public class ContextLoaderListener extends ContextLoader implements ServletContextListener { public ContextLoaderListener() {} public ContextLoaderListener(WebApplicationContext context) { super(context); } @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } @Override public void contextDestroyed(ServletContextEvent event) { closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); } }
ContextLoader这其中设置了一些变量,先只看着2个变量
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation"; public static final String CONTEXT_CLASS_PARAM = "contextClass";
然后这个类里面有一个静态代码块,加载一个默认的ContextLoader.properties文件的内容
static { // 从属性文件加载默认策略实现. // 这是目前严格内部,并不意味着要定制. try { ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class); defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException ex) { throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage()); } }
疑问:ContextLoader.properties这个配置文件不知道放在哪个位置。
初始化WebApplicationContext上下文
/** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } /** * Close the root web application context. */ @Override public void contextDestroyed(ServletContextEvent event) { closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); }
方法内容:
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { throw new IllegalStateException( "Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader* definitions in your web.xml!"); } Log logger = LogFactory.getLog(ContextLoader.class); servletContext.log("Initializing Spring root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext: initialization started"); } long startTime = System.currentTimeMillis(); try { // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); cwac.setParent(parent); } configureAndRefreshWebApplicationContext(cwac, 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); } if (logger.isDebugEnabled()) { logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]"); } if (logger.isInfoEnabled()) { long elapsedTime = System.currentTimeMillis() - startTime; logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms"); } return this.context; } catch (RuntimeException ex) { logger.error("Context initialization failed", ex); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); throw ex; } catch (Error err) { logger.error("Context initialization failed", err); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err); throw err; } }
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
相关文章推荐
- spring源码学习笔记-初始化(六)-完成及异常处理
- spring源码学习笔记-初始化(六)-完成及异常处理
- spring源码学习笔记-初始化(六)-完成及异常处理
- spring 源码学习笔记(二)—— spring ioc 之依赖注入
- Spring中AOP原理,源码学习笔记
- spring源码学习笔记-初始化(五)-MessageSource/事件监听器
- spring源码学习笔记-初始化(三)-BeanFactory
- 2016书单总结--spring-boot-in-action学习笔记--源码简析
- spring 源码学习笔记(三)—— spring aop
- Spring源码学习笔记(一)
- spring源码学习笔记-初始化(四)-PostProcessor
- spring学习笔记之AbstractController源码解读
- dubbo源码学习笔记----结合Spring
- spring源码学习笔记-初始化(三)-BeanFactory
- spring源码学习笔记-初始化(二)-BeanFactory
- ITCAST视频-Spring学习笔记(用@Resource注解完成属性装配)
- spring源码学习笔记-初始化(五)-MessageSource/事件监听器
- Spring源码学习笔记之一:从BeanPostProcesser开始
- Spring源码学习笔记(四)-Aop获取增强、切点
- spring源码学习笔记-初始化(一)-概览