web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-core.xml</param-value>
</context-param>
<listener>
<span style="white-space:pre"> </span><listener-class>com.myproject.support.ContextLoaderListener</listener-class>
<span style="white-space:pre"> </span></listener>
ContextLoaderListener
public class ContextLoaderListener extends org.springframework.web.context.ContextLoaderListener {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void contextInitialized(ServletContextEvent event) {
if( "true".equals(PropertiesUtil.get("otherFlag")) ){//当<span style="font-family: Arial, Helvetica, sans-serif;">otherFlag为true时加载spring-other.xml文件</span>
ServletContext servletContext = event.getServletContext();
try {
Field contextField = servletContext.getClass().getDeclaredField("context");
contextField.setAccessible(true);
Object context = contextField.get(servletContext);
Field parametersField = context.getClass().getDeclaredField("parameters");
parametersField.setAccessible(true);
Map parameters = (Map)parametersField.get(context);
parameters.put("contextConfigLocation", parameters.get("contextConfigLocation")+",classpath:config/spring/spring-other.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
super.contextInitialized(event);
}
}