struts和spring的整合-WebApplicationContext的取得方式

根据不同的整合方式,分别看一下WebApplicationContext的取得方式

 

1>业务Action extends ActionSupport

 

ActionSupport.java

protected final WebApplicationContext getWebApplicationContext() {
	return this.webApplicationContext;
}

protected WebApplicationContext initWebApplicationContext(ActionServlet actionServlet)
			throws IllegalStateException {

	return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, null);
}

public void setServlet(ActionServlet actionServlet) {
		super.setServlet(actionServlet);
		if (actionServlet != null) {
			this.webApplicationContext = initWebApplicationContext(actionServlet);
			this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext);
			onInit();
		}
		else {
			onDestroy();
		}
}

 

如果WebApplicationContext是通过struts plugin的方式加载的话,会直接获取default module的wac

 

2>使用DelegatingRequestProcessor

DelegatingRequestProcessor.java

private WebApplicationContext webApplicationContext;
	

public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException {
		super.init(actionServlet, moduleConfig);
		if (actionServlet != null) {
			this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig);
		}
}

protected WebApplicationContext initWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
}

protected final WebApplicationContext getWebApplicationContext() {
		return this.webApplicationContext;
}

 

 

3>所有的Action类都使用DelegatingActionProxy

DelegatingActionProxy.java

 

protected WebApplicationContext getWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
}

 

可以发现,这三种整合方式,其实都是通过DelegatingActionUtils.findRequiredWebApplicationContext方法来获取wac的

那么,我们来看一下具体的内部代码

 

public static WebApplicationContext findRequiredWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		WebApplicationContext wac = getWebApplicationContext(actionServlet, moduleConfig);
		// If no Struts-specific context found, fall back to root context.
		if (wac == null) {
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(actionServlet.getServletContext());
		}
		return wac;
}

public static WebApplicationContext getWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) {

		WebApplicationContext wac = null;
		String modulePrefix = null;

		// Try module-specific attribute.
		if (moduleConfig != null) {
			modulePrefix = moduleConfig.getPrefix();
			wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute(
					ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix);
		}

		// If not found, try attribute for default module.
		if (wac == null && !"".equals(modulePrefix)) {
			wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute(
					ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
		}

		return wac;
}

 

获取逻辑是这样的:

1>先看是否通过struts plugin的方式进行了wac的加载

2>如果没有找到,再接着看是否通过servlet或者listener的方式进行了wac的加载

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值