我需要在檢查一些參數後修改我的項目中的web.xml。我的contextConfigLocation在web.xml參數如下:如何有條件地將ContextConfigLocation添加(或修改)到web.xml?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*/test-spring-config.xml classpath:*/someContext.xml classpath:applicationContext.xml classpath:databaseContext.xml</param-value>
</context-param>
現在我需要添加類路徑:在web.xml中在檢查一些條件securityContext.xml我的contextConfigLocation之前我的應用程序加載在tomcat。 我已經在我的applicationInitializer類,它實現了ServletContextListener如下試着這樣做(部分代碼所示):
public class ApplicationInitializer implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{ ServletContext sc = event.getServletContext();
if(someConditionIsTrue){
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("classpath:securityContext.xml");
appContext.setServletContext(sc);
appContext.refresh();
sc.setAttribute("org.springframework.web.context.WebApplicationContext.ROOT",appContext);
}
但是這是行不通的,因爲我再次加載的背景下,我的web.xml爲
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
有人可以建議如何處理這個問題?任何建議表示讚賞。
我相信這就是我在我的答案中提到的;) – Prasad
是啊!我從你的答案中挖掘了以上所有內容。謝了哥們! – abhilash