使用Spring创建会话工厂对象时显示错误”

时间:2018-08-31 14:25:27

标签: spring hibernate spring-mvc

在ServletContext资源[/WEB-INF/spring-mvc-crud-demo-servlet.xml]中定义名称为'sessionFactory'的bean时出错:调用init方法失败;嵌套的异常是java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException。下面是我的XML文件

<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- Add support for component scanning -->
<context:component-scan base-package="com.code.springdemo" />

<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&amp;serverTimezone=UTC" />
    <property name="user" value="springstudent" />
    <property name="password" value="springstudent" /> 

    <!-- these are connection pool properties for C3P0 -->
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="20" />
    <property name="maxIdleTime" value="30000" />
</bean>  
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan" value="com.code.springdemo.entity" />
    <property name="hibernateProperties">
       <props>
          <prop 
    key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

<!-- Step 3: Setup Hibernate transaction manager -->
 <bean id="myTransactionManager"
     class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />

1 个答案:

答案 0 :(得分:0)

得到答案:

使用--add-modules在类路径中添加模块。

由于Java尚未从Java 9的模块中删除,因此Java仅被弃用,并且默认情况下不会在类路径上添加javax.xml.bind模块。

因此,如果要在类路径上添加javax.xml.bind,可以使用以下命令添加。

-add-modules java.xml.bind

RUN AS-> RUN Configuration->参数(在Eclipse中)