【发布时间】:2014-04-25 07:24:50
【问题描述】:
我需要在检查一些参数后修改我的项目中的web.xml。我在 web.xml 中的 contextConfigLocation 参数如下:
<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>
现在我需要在我的应用程序加载到 tomcat 之前检查一些条件,将 classpath:securityContext.xml 添加到 web.xml 中的 contextConfigLocation 中。 我已经尝试在我的 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>
有人可以建议如何处理这个问题吗?任何建议表示赞赏。
【问题讨论】:
标签: java spring jakarta-ee web-applications web.xml