启动Spring ContextLoaderListener时出错

时间:2012-12-11 02:59:31

标签: spring maven

我正在使用Spring framework 3.1.3.RELEASE& amp;开发一个简单的Web应用程序。 MAVEN。除非我从web.xml中取出监听器

,否则我的应用程序将无法启动
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Tomcat报告以下错误:

INFO: Deploying web application archive sampleapp.war
11/12/2012 3:45:03 PM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart
11/12/2012 3:45:03 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/sampleapp] startup failed due to previous errors

我的WEB.XML如下所示:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>EMS</display-name>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/*-context.xml</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

此时应用程序上下文文件为空:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">





</beans>

以下是WEB-INF / lib

下的JAR文件
eclipselink-2.4.0.jar             
javax.persistence-2.0.0.jar       
mysql-connector-java-5.1.21.jar   
spring-context-3.1.3.RELEASE.jar
spring-core-3.1.3.RELEASE.jar
spring-web-3.1.3.RELEASE.jar
spring-webmvc-3.1.3.RELEASE.jar

以下是WEB-INF

下的文件
application.properties  
spring-context.xml  
web.xml

我理解:

  • ContextLoaderListener在spring-web-3.1.3.RELEASE.jar中可用
  • 如果JAR在WEB-INF / lib中可用,那么应该选择它 由TOMCAT。

我做错了什么?

3 个答案:

答案 0 :(得分:4)

以下是您的问题:将spring-context.xml移至WEB-INF/classes并按如下方式设置上下文参数:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-context.xml</param-value>
</context-param>

WEB-INF不在CLASSPATH中。

答案 1 :(得分:1)

我分享duffmo的推理,只是想指出,如果你将配置更新为:

,你可以将应用程序上下文保留在WEB-INF目录中
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/spring-context.xml</param-value>
</context-param>

这样,您的全局应用程序上下文与调度程序配置位于同一目录中。

答案 2 :(得分:0)

问题是我的POM文件中缺少Spring Dependencies。我最初在POM中有以下依赖项

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

我在tomcat logging.properties 上启用了调试,看到我收到了一个类ava.lang.NoClassDefFoundError ,因为我添加了依赖项(一次一个)ava .lang.NoClassDefFoundError不断变化,直到满足所有依赖关系。之后ContextLoaderListener启动正常。我添加的依赖项是:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- required by Spring -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>