【问题标题】:While creating session factory object using spring it is showing error"使用 spring 创建会话工厂对象时显示错误“
【发布时间】:2019-02-06 14:38:23
【问题描述】:

在 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" />

【问题讨论】:

  • 让我猜测使用 Java 9 或 10 并且没有将 Jaxb 作为依赖项。
  • @M.Deinum 如何在不使用 maven 的情况下包含 jaxb

标签: spring hibernate spring-mvc


【解决方案1】:

得到答案:

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

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

所以如果我们想在类路径中添加 javax.xml.bind,我们可以使用以下命令添加。

--add-modules java.xml.bind

RUN AS->RUN 配置->参数(在 Eclipse 中)

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2011-12-20
    相关资源
    最近更新 更多