目录
背景介绍
在大多数使用spring的项目场景来说,都是web项目。那我们先从入口开始学习,了解spring是如何启动的。
我这里学习的spring4的代码,和spring3的有些出入。可以checkout 4.x版本观看
参考资料
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ 7.15.4
代码了解
注册Application
首先web 项目应用是通过ContextLoaderListener来注册Applicationcontext的。代码如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener代码
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());
}
具体初始化IOC容器代码
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 {
//创建上下文信息
if (this.context == null) {
//该方法逻辑:这里不贴具体代码了,太多了
//1,从servletcontext上下文中拿到对应初始化参数
//2,生成实例类
//3,确保该实例类来源于ConfigurableWebApplicationContext,否则抛出异常
//4,将这个类转成webapplicationcontext并且返回出去
this.context = createWebApplicationContext(servletContext);
}
//确保上下文信息来源了ConfigurableWebApplicationContext
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);
}
//逻辑:这里不贴具体代码了,太多了
//1,里面设置了webApplicationContext的相关属性
//2,调用webapplicationContext的refresh方法,refresh方法就是启动容器的方法
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;
}
}
}