对于struts2这样的web框架,需要整合spring来管理所有的bean,spring本身也提供了ContextLoaderListener来初始化容器。可以通过扩展ContextLoaderListener来控制spring。
[img]http://dl.iteye.com/upload/attachment/0063/8373/cffdd761-1b5e-3915-935c-1ea4dbb96fec.png[/img]
WebxApplicationContext 覆盖spring默认的Configlocations。
WebxContextLoader 负责初始化 WebxApplicationContext
WebxContextLoaderListener 负责初始化 WebxContextLoader
[img]http://dl.iteye.com/upload/attachment/0063/8373/cffdd761-1b5e-3915-935c-1ea4dbb96fec.png[/img]
WebxApplicationContext 覆盖spring默认的Configlocations。
package com.you.atlas.webx.context;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class WebxApplicationContext extends XmlWebApplicationContext {
public final static String WEBX_CONFIGURATION_LOCATION = "/WEB-INF/webx.xml";
public final static String WEBX_COMPONENT_CONFIGURATION_LOCATION_PATTERN = "/WEB-INF/webx-*.xml";
protected String[] getDefaultConfigLocations() {
if (getNamespace() != null) {
return new String[] { WEBX_COMPONENT_CONFIGURATION_LOCATION_PATTERN.replace("*", getNamespace()) };
} else {
return new String[] { WEBX_CONFIGURATION_LOCATION };
}
}
}
WebxContextLoader 负责初始化 WebxApplicationContext
package com.you.atlas.webx.context;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.context.ContextLoader;
public class WebxContextLoader extends ContextLoader {
@Autowired
protected Class<?> determineContextClass(ServletContext servletContext) throws ApplicationContextException {
return getDefaultContextClass();
}
protected Class<?> getDefaultContextClass() {
return WebxApplicationContext.class;
}
}
WebxContextLoaderListener 负责初始化 WebxContextLoader
package com.you.atlas.webx.context;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
public class WebxContextLoaderListener extends ContextLoaderListener{
protected ContextLoader createContextLoader() {
return new WebxContextLoader();
}
}