Spring boot + Envers ClassNotFoundException

Posted

技术标签:

【中文标题】Spring boot + Envers ClassNotFoundException【英文标题】: 【发布时间】:2017-08-01 01:02:37 【问题描述】:

我正在尝试将 Hibernate Envers 连接到我的 Spring Boot 应用程序中,但我得到了这个异常: 我为 Envers 尝试了很多不同的依赖项,因为这似乎是依赖项冲突问题,但没有任何帮助。 当我启动应用程序时,一切正常,但是当我尝试使用 @Audited 注释类时,此异常会下降。

org.springframework.beans.factory.BeanCreationException: Error creating bean         with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/spi/SharedSessionContractImplementor
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.base.BaseApplication.main(BaseApplication.java:11) [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.3.0.RELEASE.jar:1.3.0.RELEASE]
Caused by: java.lang.NoClassDefFoundError: org/hibernate/engine/spi/SharedSessionContractImplementor
    at org.hibernate.envers.boot.internal.EnversIntegrator.integrate(EnversIntegrator.java:94) ~[hibernate-envers-5.2.8.Final.jar:5.2.8.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:280) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.spi.SharedSessionContractImplementor
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_111]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_111]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_111]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_111]
    ... 31 common frames omitted

我的应用程序.properties

        spring.profiles.active = dev


    spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1

    # ===============================
    # = JPA / HIBERNATE
    # ===============================


    spring.jpa.show-sql = true
    logging.level.org.hibernate.type.descriptor.sql=trace

    # Hibernate ddl auto (create, create-drop, update): with "update" the database
    # schema will be automatically updated accordingly to java entities found in
    # the project
    spring.jpa.hibernate.ddl-auto = update

    # Allows Hibernate to generate SQL optimized for a particular DBMS
    spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL5InnoDBSpatialDialect
    spring.jpa.database-platform = org.hibernate.spatial.dialect.mysql.MySQLSpatial56Dialect
    spring.datasource.url = jdbc:mysql://localhost:3306/database

    # Username and secret
    spring.datasource.username = root
    spring.datasource.password = root
    server.port=8082

Gradle 构建文件:

plugins 
    id 'org.springframework.boot' version '1.5.2.RELEASE'


apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

jar 
    baseName = 'Base'
    version = '0.0.1-SNAPSHOT'


bootRun 
    systemProperties = System.properties


repositories 
    mavenLocal()
    mavenCentral()


dependencies 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '1.3.0.RELEASE'
    compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.40'
    compile group: 'org.modelmapper', name: 'modelmapper', version:'0.7.5'
    compile group: 'com.vividsolutions', name: 'jts', version:'1.13'
    compile group: 'com.bedatadriven', name: 'jackson-datatype-jts', version:'2.3'
    compile group: 'org.hibernate', name: 'hibernate-spatial', version:'5.0.11.Final'
    compile group: 'org.flywaydb', name: 'flyway-core', version:'3.2.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.4'
    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '8.5.6'
    compile group: 'io.springfox', name: 'springfox-swagger2', version:'2.4.0'
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version:'2.4.0'
    compile group: 'org.hibernate', name: 'hibernate-envers'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.4.2.RELEASE'


task wrapper(type: Wrapper) 
    gradleVersion = '1.6'

【问题讨论】:

停止混合版本。您正在混合 Spring Boot 1.5、1.4 和 1.3,并且由于包含 hibernate-spatial,所有 org.hibernate 版本都需要具有相同的版本,您的不同版本的混合会导致错误。跨度> 您使用的 Spring Boot 插件允许您“在声明 BOM 中管理的依赖项时省略版本号”。看看this。这应该有助于避免混合版本并生成更简洁和可维护的构建脚本。 【参考方案1】:

我遇到了同样的问题,我解决了添加 hibernate-core 依赖项的问题,该依赖项缺少类 (SharedSessionContractImplementor),因此您可以将其添加到您的 gradle 文件中。 我将hibernate-core 5.0.11.final 用于spring-boot 1.4.2.RELEASE

Gradle:

compile group: 'org.hibernate', name: 'hibernate-core', version: '5.0.11.Final'

【讨论】:

以上是关于Spring boot + Envers ClassNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 和 Hibernate Envers:手动更新 AUD-Table

SCD (slowly changed dimension) Type 4/6 - 如何在spring boot app, hibernate envers中实现

在 Spring Hibernate java 项目中使用“Envers”审计表

hibernate-envers RevisionListener spring 集成为 spring bean

Spring 3.x 和 Hibernate Envers

如何强制 Hibernate Envers 在 Spring @Transactional 方法中提交修订