Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 格雷德尔和;SLF4J拒绝排除可传递依赖项将导致IllegalStateException_Spring_Gradle_Log4j_Slf4j - Fatal编程技术网

Spring 格雷德尔和;SLF4J拒绝排除可传递依赖项将导致IllegalStateException

Spring 格雷德尔和;SLF4J拒绝排除可传递依赖项将导致IllegalStateException,spring,gradle,log4j,slf4j,Spring,Gradle,Log4j,Slf4j,在项目上运行单元测试时,我遇到此错误: Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more det

在项目上运行单元测试时,我遇到此错误:

Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
    at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54)
    ... 29 more
我可以通过以下方式停止错误:

configurations {
  all*.exclude group: "org.slf4j"

}
但是我在运行时得到了
ClassNotFoundExceptions
,因为需要slf4j(及其适配器)


有什么想法吗?我只想从Spring的依赖项中排除slf4j。我在这个问题上看到了很多帖子,但没有解决它——我的排除是否正确?

您的问题是,您拥有
log4j-over-slf4j
,它将所有
log4j
日志重定向到
slf4j
slf4j-log4j12
,将所有
slf4j
日志输出到类路径中的
log4j
。这意味着你会有一个无休止的循环,这当然是错误的

所以你必须决定你想要什么。是否要将所有
log4j
日志重定向到
slf4j
,然后将所有
slf4j
日志定向到其他日志框架(或
slf4j简单
,例如,用于登录到
System.out
或不绑定以抑制日志记录),还是要将所有
slf4j
日志定向到
log4j
,那么,首先将
log4j
日志重定向到
slf4j
是没有意义的

如何正确解决问题在很大程度上取决于预期的结果。

您还可以使用gradle任务
dependencyInsight

找出是什么拉入了依赖项,我刚刚排除了
spring boot starter日志记录

configurations {
    all*.exclude module: 'spring-boot-starter-logging'
}
我的依赖项现在如下所示:

dependencies {
    compile "org.springframework:spring-core:$springVersion"
    compile "org.springframework:spring-jdbc:$springVersion"
    compile "org.springframework:spring-orm:$springVersion"
    compile "org.springframework:spring-tx:$springVersion"
    compile "org.springframework.data:spring-data-jpa:1.10.1.RELEASE"
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
    compile "org.apache.velocity:velocity:1.7"
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
    testCompile("junit:junit")
    testCompile "org.springframework.boot:spring-boot-starter-test"
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

我其实没有偏好,我只是想让它工作!让我们来看看log4j——我如何解决它呢?
configurations.all{exclude group:'org.slf4j',module:'slf4j-over-slf4j'}
btw。您不需要排除
slf4j api
,如果您想要记录使用java commons日志的libs,您不应该排除
jcl-over-slf4j
。当然,还要删除对
log4j-over-slf4j
的显式依赖。然后添加
slf4j-log4j12
并根据需要配置
log4j
。谢谢您的帮助。我删除了spring boot starter日志记录,它把一切都整理好了。太好了,那么请阅读并遵守:-)我在下面添加了我的内容以获取更多信息,只是忘记接受你的。无意伤害:)
dependencies {
    compile "org.springframework:spring-core:$springVersion"
    compile "org.springframework:spring-jdbc:$springVersion"
    compile "org.springframework:spring-orm:$springVersion"
    compile "org.springframework:spring-tx:$springVersion"
    compile "org.springframework.data:spring-data-jpa:1.10.1.RELEASE"
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
    compile "org.apache.velocity:velocity:1.7"
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
    testCompile("junit:junit")
    testCompile "org.springframework.boot:spring-boot-starter-test"
    testCompile group: 'junit', name: 'junit', version: '4.11'
}