调用init方法失败;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / util / DTDEntityResolver

时间:2013-02-04 07:02:14

标签: spring hibernate spring-mvc

我在maven项目中使用带有spring的hibernate。这是我的POM文件

<?xml version="1.0" encoding="UTF-8"?>
<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>org.mod.com</groupId>
    <artifactId>Services</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Services</name>
    <url>http://maven.apache.org</url>

    <properties>
        <maven.test.failure.ignore>true</maven.test.failure.ignore>
        <spring.framework.version>3.1.3.RELEASE</spring.framework.version>
        <hibernate.version>4.1.1.Final</hibernate.version>
        <sl4j.version>1.5.6</sl4j.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                    <sourceIncludes>
                        <sourceInclude>**/*.*</sourceInclude>
                    </sourceIncludes>
                </configuration>
            </plugin>
            <!-- Configuration which allows JUnit tests to be placed in the same folder as java files
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
                    <testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
                </configuration>
            </plugin> -->
        </plugins>
        <!-- Configuration which allows configuration files (such as xml files) to be placed in the same folder as java files
        <resources>
           <resource>
             <directory>${basedir}/src/main/java/</directory>
           </resource>
         </resources> -->
    </build>


    <dependencies>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
          <scope>provided</scope>
        </dependency>
        <!-- <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.1</version>
          <scope>provided</scope>
        </dependency>
        **********************************************************************
        ** SPRING DEPENDENCIES **
        ********************************************************************** -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument-tomcat</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-struts</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>

        <!-- **********************************************************************
        ** POSTGRES DEPENDENCIES **
        ********************************************************************** -->
        <dependency>
                <groupId>postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>8.3-603.jdbc4</version>
        </dependency>

        <!-- **********************************************************************
        ** HIBERNATE DEPENDENCIES **
        ********************************************************************** -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- **********************************************************************
        ** JAVAX PERSISTENCE **
        ********************************************************************** -->
        <!-- <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>${javax.persistence.version}</version>
        </dependency> -->

        <!-- **********************************************************************
        ** OTHER DEPENDENCIES **
        ********************************************************************** -->      
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jcl</artifactId>
            <version>${sl4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${sl4j.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
            <scope>runtime</scope>
        </dependency>

        <!-- **********************************************************************
        ** TEST DEPENDENCIES
        ** ********************************************************************** -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>

    </dependencies>
</project>

这是我的DAO课程

public class Task_Impl implements Task_Interface {

    @Autowired
    SessionFactory sessionfactory;
    @Override
    @Transactional
    public void createTask(Task task) {
        // TODO Auto-generated method stub
        sessionfactory.getCurrentSession().save(task);
        System.out.println("Task created in database");


    }

    @Override
    public void deleteTask() {
        // TODO Auto-generated method stub

    }

}

这是我的Dispatcher Servlet(HelloWeb-servlet.xml)

   xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" 

   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">


<context:component-scan base-package="org.mod.com.Prime"/>
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
<bean id="task_impl_bean" class="org.mod.Prime.DAO.Implementation.Task_Impl">

</bean>

<bean id="sessionfactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceBean"></property>
    <property name="annotatedClasses" value="org.mod.Prime.Data.Task"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/newdatabase"/>
    <property name="username" value="postgres"/>
    <property name="password" value="P@ssword"/>
</bean>

 <tx:annotation-driven transaction-manager="txManager"/>

  <bean id="txManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionfactory">
         <ref local="sessionfactory"/>
    </property>
  </bean>

当我尝试运行项目时,我收到以下错误

Error creating bean with name 'task_impl_bean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory org.mod.Prime.DAO.Implementation.Task_Impl.sessionfactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionfactory' defined in ServletContext resource [/WEB-INF/HelloWeb-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver

主要根本原因是

Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver

正如你在我的pom文件中看到的,我已经包含了hibernate jar。我不知道确切的错误。有人能帮助我吗?提前致谢

2 个答案:

答案 0 :(得分:1)

尝试在sessionFactory

之前声明dataSourceBean
<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/newdatabase"/>
    <property name="username" value="postgres"/>
    <property name="password" value="P@ssword"/>
</bean>

<bean id="sessionfactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceBean"></property>
    <property name="annotatedClasses" value="org.mod.Prime.Data.Task"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

答案 1 :(得分:1)

这是Bean init时的动态类加载!在加载到容器中时解析Spring Bean类时使用DTD实体解析器。我认为你的Hibernate版本与你的框架和代码不兼容!你为什么使用不同的hibernate-annotations版本?

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

您确定您的Hibernate版本与Spring框架兼容吗?尝试更改版本,还要检查用于运行Spring服务器的JVM版本!