NoClassDefFoundError:无法初始化类org.hibernate.cfg.AnnotationConfiguration

时间:2016-10-19 10:02:57

标签: java spring-mvc

我正在使用Spring MVC和Hibernate。 当我启动Webapp时,它会在创建sessionFactory bean时抛出异常:

我的pom.xml

<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>com.rohit.common</groupId>
    <artifactId>myProj</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myProj Tmp</name>

    <properties>
        <jdk.version>1.8</jdk.version>
        <spring.version>3.0.0.RELEASE</spring.version>
        <spring-orm.version>3.0.0.RELEASE</spring-orm.version>
        <spring-jdbc.version>3.0.0.RELEASE</spring-jdbc.version>
        <jstl.version>1.2</jstl.version>
        <servletapi.version>2.5</servletapi.version>
        <hibernate.version>3.2.3.ga</hibernate.version>
    </properties>

    <dependencies>

        <!-- Spring MVC framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring-orm.version}</version>
        </dependency>
        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- for compile only, your container should have this -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servletapi.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring AOP dependency -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
        <!-- Spring JDBC drivewr -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring-jdbc.version}</version>
        </dependency>
        <!-- Hibernate framework -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- Hibernate library dependecy start -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.4.0.GA</version>
        </dependency>

        <!--<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-tools</artifactId>
            <version>4.3.1.CR1</version>
        </dependency>
        -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.4.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.1.GA</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.8</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
         <!-- Hibernate library dependecy end -->

    </dependencies>

</project>

错误堆栈跟踪:

  

java.lang.NoClassDefFoundError:无法初始化类org.hibernate.cfg.AnnotationConfiguration       sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)       sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       java.lang.reflect.Constructor.newInstance(Constructor.java:422)       org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)       org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)       org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfiguration(LocalSessionFactoryBean.java:813)       org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:549)       org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398)       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)       org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:290)       org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)       org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)       org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)       org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:540)       org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)       org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)       org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)       org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)       org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)       org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)       org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)       javax.servlet.GenericServlet.init(GenericServlet.java:158)       org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)       org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)       org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)       org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)       org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:658)       org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:222)       org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1566)       org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523)       java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)       org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

似乎你混合了一些库。尝试添加hibernate-core

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.10.Final</version>
</dependency>

而不是:

<!-- Hibernate framework -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>${hibernate.version}</version>
</dependency>
<!-- Hibernate library dependecy start -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.4.0.GA</version>
</dependency>

关键是hibernate-core已包含hibernate-annotations依赖项,它会自动下载,否则会导致依赖项冲突。