【问题标题】:Personalized ApplicationContextInitializer can be the cause of Error Could not open ServletContext resource [/WEB-INF/applicationContext.xml]?个性化的 ApplicationContextInitializer 可能是 Error Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 的原因?
【发布时间】:2014-08-17 22:19:57
【问题描述】:

我收到一个错误,我正在部署的应用程序找不到 /WEB-INF/applicationContext.xml。

这是我的 web.xml

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>com.sheidaei.chnlsales.web.util.P13nApplicationContextInitializer</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.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>

这是自定义类:

package com.sheidaei.chnlsales.web.util;

import java.io.IOException;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.support.ResourcePropertySource;
import org.apache.log4j.Logger;

public class P13nApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    protected static Logger LOG = Logger.getLogger(P13nApplicationContextInitializer.class);

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        try {                   
            environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties"));
            //environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:root-context.xml"));
            environment.getPropertySources().addFirst(new ResourcePropertySource("/WEB-INF/spring/root-context.xml"));
            environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:fw-refresh-spring.xml"));
            LOG.info("env.properties loaded");
        } catch (IOException e) {
            // it's ok if the file is not there. we will just log that info.
            LOG.info("didn't find env.properties in classpath so not loading it in the AppContextInitialized");
        }
    }

}

我试图根据这里的讨论创建一个个性化的 ApplicationContextInitializer:How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property 但我没有成功。

在更改为自定义类之前,我的 web.xml 如下所示(并且可以成功部署应用程序)

<context-param>
    <param-name>contextConfigLocation</param-name>          
    <param-value>/WEB-INF/spring/root-context.xml, 
        classpath:fw-refresh-spring.xml
    </param-value> 
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
-->

【问题讨论】:

  • 请发布完整的堆栈跟踪。

标签: java xml spring servlets weblogic


【解决方案1】:

ApplicationContextInitializer 可用于配置配置文件和属性源(.properties 文件),但不能用于配置 Spring 配置文件(.xml 文件)。

远程以下行:

environment.getPropertySources().addFirst(new ResourcePropertySource("/WEB-INF/spring/root-context.xml"));
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:fw-refresh-spring.xml"));

并返回contextConfigLocationweb.xml

<context-param>
    <param-name>contextConfigLocation</param-name>          
    <param-value>/WEB-INF/spring/root-context.xml, 
        classpath:fw-refresh-spring.xml
    </param-value> 
</context-param>

【讨论】:

  • 谢谢@axtavt。那么我可以在 web.xml 中有多个 吗?
  • 可以,只要他们有不同的&lt;param-name&gt;s
猜你喜欢
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多