spring - 如何从 Spring Boot 中排除依赖项

标签 spring hibernate gradle spring-boot spring-data-jpa

我正在使用 spring boot,以下是我的 gradle 文件

    buildscript {
    ext {
        springBootVersion = '2.0.0.BUILD-SNAPSHOT'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa:1.2.1.RELEASE')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("com.h2database:h2")
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

同时在 gradle 文件中添加以下依赖项
org.springframework.boot:spring-boot-starter-data-jpa:1.2.1.RELEASE

它包括一堆其他依赖项,例如 hibernate n,我现在不需要它(只是想使用 spring data jpa),这会导致许多其他问题
那么我如何只使用 spring-data-jpa 及其相关的依赖项?

试图禁用像 exclude = { HibernateJpaAutoConfiguration.class} 但不顺利

提前谢谢

最佳答案

我想最简单的解决方案就是包含 spring-data-jpa ,不是 spring-boot-starter-data-jpa :

compile('org.springframework.data:spring-data-jpa:2.0.0.M2')

或者,如果您真的想离开首发,那么您可以尝试执行以下操作:
compile('org.springframework.boot:spring-boot-starter-data-jpa') {
    exclude(module: 'hibernate-core')
    exclude(module: 'hibernate-entitymanager')
}

但是明白这一点,才能使用spring-data-jpa你必须有一个像 hibernate 这样的持久提供者,只是因为spring-data-jpa本身只不过是 JPA 之上的抽象。这反过来又是一个抽象,也是在像 hibernate 这样的持久性提供者之上的。或 eclipselink .

更新

如果您想在 gradle 构建脚本中保留所有 jpa 依赖项,但又不想 spring-boot要使用它们一段时间,然后您必须同时禁用 DataSourceAutoConfigurationHibernateJpaAutoConfiguration以及。
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

关于spring - 如何从 Spring Boot 中排除依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44454723/

相关文章:

java - NullPointerException 发生在 FetchType.LAZY withi hibernate 和 spring boot

c# - 检索最近 5 个订单的单位

java - 如何以 spring mvc 形式绑定(bind) Set<CustomObject>

java - 在 @ResponseStatus 中使用多个值 HttpStatus

java - 如何在 Spring Boot 中处理两个模型之间的多对多关系并围绕它们设计剩余调用?

hibernate - 使用 transient 字段在 grails 中查询和排序数据库结果

gradle - 反向构建依赖关系

android - 使用FirebaseOptions.Builder()的setServiceAccount()的编译时错误

java - 无法在全新安装的 Android Studio 0.8.1 上启动 Gradle 守护进程

java - 如何使用包含存储库 bean 的自定义 validator 注释验证 @PathVariable