调用init方法失败;嵌套异常是java.lang.NoClassDefFoundError

时间:2018-05-20 22:26:39

标签: java spring hibernate

我收到以下错误:

  

引起:org.springframework.beans.factory.BeanCreationException:创建名称为' profileSessionFactory'的bean时出错在类路径资源[config-beans.xml]中定义:init方法的调用失败;嵌套异常是java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1702)           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579)           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)           在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:317)           在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)           在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)           在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)           在org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)           在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)           在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)           at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)           ......还有74个   引起:java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException           在org.hibernate.boot.spi.XmlMappingBinderAccess。(XmlMappingBinderAccess.java:43)           在org.hibernate.boot.MetadataSources。(MetadataSources.java:87)           在org.springframework.orm.hibernate5.LocalSessionFactoryBean.getMetadataSources(LocalSessionFactoryBean.java:388)           在org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:423)           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761)           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698)           ......还有84个   引起:java.lang.ClassNotFoundException:javax.xml.bind.JAXBException           在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)           在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1136)           ......还有90多个

这是我的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>project.web</groupId>
 <artifactId>start</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>start Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <properties>
     <maven.compiler.source>1.6</maven.compiler.source>
     <maven.compiler.target>1.6</maven.compiler.target>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <spring.version>5.0.5.RELEASE</spring.version>
     <hibernate.version>5.2.17.Final</hibernate.version>
     <c3p0.version>0.9.5.2</c3p0.version>
     <hibernate.validator>5.4.1.Final</hibernate.validator>
     <jstl.version>1.2.1</jstl.version>
     <tld.version>1.1.2</tld.version>
     <junit.version>4.12</junit.version>
     <servlet.version>3.1.0</servlet.version>
     <servlet.version>3.0.1</servlet.version>
     <mysql.connector.version>5.1.9</mysql.connector.version>
 </properties>
 <dependencies>
     <!-- Junit -->
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>${junit.version}</version>
         <scope>test</scope>
     </dependency>
     <!--Servlet-Api -->
     <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>${servlet.version}</version>
         <scope>provided</scope>
     </dependency>
     <!-- Spring Framework -->
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-beans</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-jdbc</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>${spring.version}</version>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.5</version>
     </dependency>
     <!-- Hibernate dependencies -->
     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3p0.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator}</version>
    </dependency>
    <!-- JSTL Dependency -->
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>javax.servlet.jsp.jstl-api</artifactId>
        <version>${jstl.version}</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>${tld.version}</version>
    </dependency>
     <!-- Servlet API -->
     <!-- MySQL database driver -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>${mysql.connector.version}</version>
     </dependency>
     <!-- Rabbit dependencies -->
     <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>1.7.4.RELEASE</version>
     </dependency>
     <dependency>
        <groupId>com.rabbitmq</groupId>
        <artifactId>amqp-client</artifactId>
        <version>4.0.0</version>
     </dependency>
 </dependencies>
 <build>
     <finalName>start</finalName>
     <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
                <server>myserver</server>
                <url>http://localhost:8080/manager/text</url> 
            </configuration>
        </plugin>
     </plugins>
 </build>

这是我的bean定义文件:

    <?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 
    http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <context:component-scan base-package="start.web" />

    <context:property-placeholder location="classpath:users.db.properties, classpath:users.rabbit.properties" />

    <!-- data source -->
    <bean id="profileDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${datasource.driver}" />
        <property name="url" value="${datasource.url}" />
        <property name="username" value="${datasource.username}" />
        <property name="password" value="${datasource.password}" />
    </bean> 

    <!-- Hibernate SessionFactory -->
    <bean id="profileSessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="profileDataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.id.new_generator_mappings">${hibernate.id.new_generator_mappings}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.connection.zeroDateTimeBehavior">convertToNull</prop>
            </props>
        </property>
        <property name="packagesToScan" value="start.web.model.registration"></property>
    </bean>

    <!-- The transaction manager -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="profileSessionFactory" />
    </bean>

    <!-- Rabbit connections beans -->
    <rabbit:connection-factory id="emailAccountConnectionFactory" host="${users.host}"/>

    <rabbit:template id="amqpTemplate" connection-factory="emailAccountConnectionFactory"
        exchange="${users.account.exchange}" routing-key="${users.account.routingKey}"/>

    <!--  <rabbit:admin connection-factory="emailAccountConnectionFactory" />-->

    <rabbit:queue id="emailAccount" name="${users.account.queue.name}"/>

    <rabbit:direct-exchange name="${users.account.exchange}">
        <rabbit:bindings>
            <rabbit:binding queue="emailAccount" key="${users.account.routingKey}" />
        </rabbit:bindings>
    </rabbit:direct-exchange>


    <rabbit:listener-container connection-factory="emailAccountConnectionFactory" acknowledge="none">
        <rabbit:listener ref="emailAccountAckListener" queue-names="${users.account.queue.name}" />
    </rabbit:listener-container>

</beans>

我在之前的帖子中已经读过,hibernate和spring版本可能不兼容,这可能会引起这个问题,但我不确定兼容性的正确版本是什么。我使用的是Spring 5.0.5.Release和Hibernate 5.2.17.Final。如果有合适的解决方法,请告知我们

1 个答案:

答案 0 :(得分:2)

您可以尝试在pom中添加以下依赖项吗?

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.6</version>
</dependency>