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的加载

 

你可能感兴趣的:(struts,spring)