java - 个性化的 ApplicationContextInitializer 可能会导致错误无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]?

标签 java xml spring servlets weblogic

我收到错误消息,指出我正在部署的应用程序找不到/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>
-->

最佳答案

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"));

并将contextConfigLocation返回到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>

关于java - 个性化的 ApplicationContextInitializer 可能会导致错误无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441096/

相关文章:

java - 为什么 iReports 预览中不显示货币格式

java - 是否有可能在 Android 或黑莓上运行小程序?

java - 使用 JDBC 插入 Microsoft SQL

java - 使用属性获取元素

java - 使用 RestTemplate 进行外部 REST 端点调用时出错

spring - 如何使用 Spring JMS 在 ActiveMQ 中创建多个监听器

java - 显式和隐式 Intent

c# - 更改代码段中文字的顺序

c# - 如何将 .xml 文件与 xmlns 属性一起反序列化?

java - 如何在特定时间间隔调用作为 Web 应用程序部署到 Apache Tomcat 服务器的 Java 方法?