1. 重构 ContextLoaderListener ;
2. 继承ContextLoader ,重写 protected WebApplicationContext createWebApplicationContext(
ServletContext servletContext, ApplicationContext parent)
throws BeansException {};方法;
code:
public class ContextLoaderListener implements ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new CasContextLoader();
}
/**
* Return the ContextLoader used by this listener.
* @return the current ContextLoader
*/
public ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
}
public class CasContextLoader extends ContextLoader {
protected WebApplicationContext createWebApplicationContext(
ServletContext servletContext, ApplicationContext parent)
throws BeansException {
// return super.createWebApplicationContext(servletContext, parent);
Class contextClass = determineContextClass(servletContext);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
wac.setParent(parent);
wac.setServletContext(servletContext);
String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
String flag=CasConstants.IS_USE_MEMCACHED;
if (configLocation != null) {
String[] xml=StringUtils.tokenizeToStringArray(configLocation,ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS);
if(flag!=null && flag.equalsIgnoreCase("true")){
String[] new_xml=StringUtils.addStringToArray(xml, "/WEB-INF/cas_memcached.xml");
wac.setConfigLocations(new_xml);
}else{
wac.setConfigLocations(xml);
}
}
wac.refresh();
return wac;
}
}
------君临天下,舍我其谁------