我正在使用 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
要使用它们一段时间,然后您必须同时禁用 DataSourceAutoConfiguration
和 HibernateJpaAutoConfiguration
以及。@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
关于spring - 如何从 Spring Boot 中排除依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44454723/