使用Spring MVC和Hibernate与JPA的ContextLoaderListener错误

时间:2014-01-16 17:39:23

标签: spring hibernate spring-mvc jpa

我研究并尝试了有关此问题的其他问题的解决方案,但没有成功(例如下面的主题)

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

我是.NET开发人员,尝试使用我当前的项目切换到JAVA。我使用JPA作为我的数据存储库层(使用Maven),使用Hibernate配置Spring MVC项目。我已经配置了所需的文件(web.xml,pom文件,jpa上下文文件等)。但是,一旦将其部署到TomCat Apache服务器,我就会收到一条错误消息,指出配置应用程序监听器时出现问题。

我尝试了一些步骤:

  1. 我的Spring Maven依赖项列为编译而非提供
  2. 我在构建路径中包含了Maven依赖项
  3. 我使用Maven菜单更新了项目
  4. 我检查过以确保Spring依赖关系包含在依赖关系层次结构
  5. 你能帮助我吗?提前谢谢!

    完整错误:

    SEVERE: Error configuring application listener of class org.springframework.web.context.contextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.contextLoaderListener at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:142) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4854) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

    这似乎是一个相当常见的错误 - 然而,到目前为止提出的解决方案还没有奏效。

    下面是我的Pom文件:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>mil.navy.navsupbsc.contacts</groupId>
        <artifactId>contacts</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>contacts Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <properties>
    
        <junit.version>4.11</junit.version>
        <spring.version>3.2.0.RELEASE</spring.version>
        <jackson.version>1.9.10</jackson.version>
        <hibernateValidator.version>4.2.0.Final</hibernateValidator.version>
        <commonsDbcp.version>1.4</commonsDbcp.version>
        <hibernateEntityManager.version>4.1.9.Final</hibernateEntityManager.version>
        <javaxTransactionJta.version>1.1</javaxTransactionJta.version>
    </properties>
    <dependencies>
        <!-- Added this dependency due to web search solutions for this problem -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${commonsDbcp.version}</version>
        </dependency>
        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernateValidator.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>
    
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    
        <!-- JPA Dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernateEntityManager.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>${javaxTransactionJta.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Oracle Dependencies -->
    </dependencies>
    <build>
        <finalName>contacts</finalName>
    </build>
    </project>
    

    和我的web.xml文件:

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
    <display-name>Archetype Created Web Application</display-name>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/jpaContext.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.contextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Contact</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Contact</servlet-name>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>
        <servlet-mapping>
        <servlet-name>Contact</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    

    我的persistence.xml文件

    <persistence xmlns="http://java.sun.com/xml/ns/persistence/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence_2_0.xsd"
        version="2.0">
    
    <persistence-unit name="punit">
    
    </persistence-unit>
    
    </persistence>
    

    我的jpa上下文文件:

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
    <context:annotation-config />
    
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="punit" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                <entry key="hibernate.hbm2ddl.auto" value="none" />
                <entry key="hibernate.format_sql" value="true" />
            </map>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="jdbc:oracle:thin:@//###.###.##.##:####/xxxxx" />
        <property name="username" value="xxxxx" />
        <property name="password" value="xxxxx"/>
    </bean>
    

1 个答案:

答案 0 :(得分:3)

我认为你的web.xml定义中有一个拼写错误。它必须是

org.springframework.web.context.ContextLoaderListener

资本 C

基本上,这个......

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

必须是

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