我想覆盖上下文 configLocation
web.xml 如下
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>com.mypackage.MyDispacherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:default-ctx.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
然后是 MyDispacherServlet
public class MyDispacherServlet extends org.springframework.web.servlet.DispatcherServlet {
@Override
public void init(ServletConfig config) throws ServletException {
// here will be code to find dynamically other-ctx.xml
String correctSpringXml = "classpath*:other-ctx.xml";
setContextConfigLocation(correctSpringXml) ;
super.init(config);
}
@Override
protected WebApplicationContext initWebApplicationContext() throws BeansException {
WebApplicationContext wac = super.initWebApplicationContext();
return wac;
}
}
但是这段代码不起作用。如何正确覆盖 contextConfigLocation?