配置 Spring JPA:无法实例化 [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]

Posted

技术标签:

【中文标题】配置 Spring JPA:无法实例化 [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]【英文标题】:Configure Spring JPA: Failed to instantiate [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter] 【发布时间】:2018-08-20 00:57:12 【问题描述】:

我正在尝试配置一个 Spring Data JPA 项目,配置如下:

applicationContext.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:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        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-4.3.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.3.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- Using Spring annotations -->
    <tx:annotation-driven />

    <!-- Activate scanning of @Autowired -->
    <context:annotation-config />

    <!-- Activate scanning of @Repository -->
    <context:component-scan base-package="boo.com" />

    <!-- Active scanning Repositories -->
    <jpa:repositories base-package="boo.com.repo"></jpa:repositories>

    <!-- Load JDBC properties -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:jdbc.properties" />

    <!-- Configure C3P0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" p:driverClass="$jdbc.driverClassName"
        p:jdbcUrl="$jdbc.databaseurl" p:user="$jdbc.username" p:password="$jdbc.password" />

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
        <property name="databasePlatform" value="org.hibernate.dialect.mysql5Dialect" />
    </bean>

    <!-- Not sure if it's needed -->
    <bean id="persistenceUnitManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="defaultDataSource" ref="dataSource" />
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <!-- Configure EntityManagerFactory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <!-- Scannig model -->
        <property name="packagesToScan">
            <list>
                <value>boo.com.model</value>
            </list>
        </property>

        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">3000</prop>
            </props>
        </property>

    </bean>

    <!-- Configure transaction manager -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>

</beans>

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>CRUD-SpringData</groupId>
    <artifactId>CRUD-SpringData</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Brussels-SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>5.2.14.Final</version>
        </dependency>

    </dependencies>

</project>

当我尝试部署项目时,它抛出了一个错误:

线程“main”中的异常 org.springframework.beans.factory.BeanCreationException:错误 创建在类路径中定义的名称为“entityManagerFactory”的bean 资源 [applicationContext.xml]:无法解析对 bean 的引用 'jpaVendorAdapter' 同时设置 bean 属性'jpaVendorAdapter'; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建在类路径中定义的名称为“jpaVendorAdapter”的 bean 资源 [applicationContext.xml]:bean 的实例化失败; 嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: 构造函数抛出异常;嵌套异常是 java.lang.NoClassDefFoundError: org/hibernate/HibernateException at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) 在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) 在 org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:140) 在 org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:84) 在 Test.main(Test.java:16) 引起: org.springframework.beans.factory.BeanCreationException:错误 创建在类路径中定义的名称为“jpaVendorAdapter”的 bean 资源 [applicationContext.xml]:bean 的实例化失败; 嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: 构造函数抛出异常;嵌套异常是 java.lang.NoClassDefFoundError: org/hibernate/HibernateException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ... 15 更多原因: org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: 构造函数抛出异常;嵌套异常是 java.lang.NoClassDefFoundError: org/hibernate/HibernateException at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) 在 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ... 23 更多原因:java.lang.NoClassDefFoundError: org/hibernate/HibernateException 在 org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.(HibernateJpaVendorAdapter.java:66) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 方法)在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)在 java.lang.reflect.Constructor.newInstance(未知来源) 在 org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ... 25 更多原因:java.lang.ClassNotFoundException: org.hibernate.HibernateException 在 java.net.URLClassLoader.findClass(未知来源)在 java.lang.ClassLoader.loadClass(Unknown Source) 在 sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 31 更多

谁能告诉我我错过了什么?我真的很感激。

[更新]

我尝试如下更新项目:

添加休眠核心依赖 将platform-b​​om 版本从Brussels-SR7 更改为Brussels-SR6 将 C3p0 更改为 JDBC

所有这些更改都不起作用并继续抛出相同的错误。

根本原因 该错误来自 Maven 依赖项。我删除了 ./m2 并重建了项目。现在工作正常。不知道如何在项目中检测到此类问题。

【问题讨论】:

【参考方案1】:

如果你查看 stackTrace (java.lang.ClassNotFoundException: org.hibernate.HibernateException) 那么它表明它无法加载 HibernateException。该类在 hibernate-core 依赖项中。

请在您的 pom.xml 中包含以下内容

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
</dependency>

这应该可以解决您的问题。

【讨论】:

您是否遇到同样的异常?

以上是关于配置 Spring JPA:无法实例化 [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]的主要内容,如果未能解决你的问题,请参考以下文章

运行spring boot应用报错:无法实例化接口org.springframework.context.ApplicationListener

Spring Boot 测试:无法实例化内部配置类

Spring Data JPA:一对一实例化问题:PersistentObjectException:分离实体传递给持久化

当我尝试使用 Hibernate ogm 和 spring boot 时,控制台给出“无法实例化命名策略类”错误

CDI + JPA 多个 @OneToMany - LAZY/EAGER - LazyInitializationException 或无法实例化多个包

JPA - 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]