Spring源码(2)Context篇之WebApplicationContext

上一篇讲解了Spring加载Context的起源来自工程下web.xml配置的Listener

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这一篇接着往下探索Spring Context的神秘…

上一篇讲到了ContextLoader类以及它的方法:initWebApplicationContext(),看方法名就知道是用来初始化一个WebApplicationContext

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) {
   
   
		 //且看这一句,用来生成一个WebApplicationContext
		 this.context = createWebApplicationContext(servletContext);
	 }
	 if (this.context instanceof ConfigurableWebApplicationContext) {
   
   
		ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值