webapplication 初始化

在web.xml中配置了ContextLoaderListener。
定义如下:
public class ContextLoaderListener extends ContextLoader [color=red]implements ServletContextListener [/color]{

因为实现了ServletContextListener 接口,所以在web容器启动的时候,就会执行该接口的方法contextInitialized()。

方法中this.contextLoader.initWebApplicationContext(event.getServletContext());
开始初始化。

ApplicationContext parent = loadParentContext(servletContext);
先取得parent 。在我经历的项目中,parent 没有配置。先不看。

然后是this.context = createWebApplicationContext(servletContext, parent);
然后是
Class<?> contextClass = determineContextClass(sc);
然后是 contextClassName = [color=red]defaultStrategies.getProperty(WebApplicationContext.class.getName());[/color]
try {
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
}

其中defaultStrategies会在static初始段初始化,它会默认去读spring的jar中的ContextLoader.properties文件。文件内容如下:
# Default WebApplicationContext implementation class for ContextLoader.
# Used as fallback when no explicit context implementation has been specified as context-param.
# Not meant to be customized by application developers.

[color=red]org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext[/color]


所以框架默认的是XmlWebApplicationContext。

所以返回的contextClass是XmlWebApplicationContext实例。

接下来执行 ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

这个的目的就是利用XmlWebApplicationContext的构造方法,返回一个XmlWebApplicationContext实例。

其中的方法return [color=red]instantiateClass[/color](clazz.getDeclaredConstructor());
描述如下:
Convenience method to instantiate a class using the given constructor. As this method doesn't try to load classes by name, it should avoid class-loading issues.

[color=red]Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor[/color].

具体我还不是很了解,待续。

接下来是 wac.setParent(parent);
wac.setServletContext(sc);//保存sc的引用
[color=red]wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));[/color]
其中的CONFIG_LOCATION_PARAM的值为
public static final String CONFIG_LOCATION_PARAM = "[color=red]contextConfigLocation[/color]";
这也是为什么我们的bean的定义文件必须配置在param为这个的下边的原因。

接下来是customizeContext(sc, wac);这个没看到具体实现,估计是扩展用的。

接下来就是最重要的最后一步wac.refresh();

待续。。。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值