我真的很努力地将logback-classic的加载作为一个传递依赖问题。我遵循此处关于堆栈溢出的建议,但是,它不断重复发生。
我使用 maven 排除项来尝试控制它,它根本没有出现在我的 pom 依赖项层次结构中。然而它仍然被加载!
这是我的运行时跟踪
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/david/.m2/repository/de/ruedigermoeller/kontraktor-http/3.33/kontraktor-http-3.33.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/david/.m2/repository/ch/qos/logback/logback-classic/1.2.1/logback-classic-1.2.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
W 13:22:24:837 : DispatcherThread 1 : DispatcherThread :
java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getLoggerFactoryClassStr()Ljava/lang/String;
at org.slf4j.LoggerFactory.reportActualBinding(LoggerFactory.java:271)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:143)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:120)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:331)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:283)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:304)
at org.quartz.impl.StdSchedulerFactory.<init>(StdSchedulerFactory.java:284)
at org.quartz.impl.StdSchedulerFactory.getDefaultScheduler(StdSchedulerFactory.java:1539)
at uk.co.example.ExampleEwsService.init(ExampleEwsService.java:296)
部分问题在于 Kontraktor 框架覆盖了 StaticLoggerBinder.getLoggerFactoryClassStr 方法。但是很容易确保它首先被加载,然后从正在加载它的石英中排除logback经典,如下所示:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency>
此时,slf4j 仅由 kontraktor 加载。 logback-classic 在 dependency:tree 中没有出现
[INFO] +- de.ruedigermoeller:kontraktor:jar:3.33:compile
[INFO] | +- org.jctools:jctools-core:jar:1.0:compile
[INFO] | - de.ruedigermoeller:fst:jar:2.44.5:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.5.3:compile
[INFO] | +- org.javassist:javassist:jar:3.19.0-GA:compile
[INFO] | +- org.objenesis:objenesis:jar:2.1:compile
[INFO] | - com.cedarsoftware:java-util:jar:1.9.0:compile
[INFO] | - com.cedarsoftware:json-io:jar:2.5.1:compile
[INFO] +- de.ruedigermoeller:kontraktor-http:jar:3.33:compile
[INFO] | +- io.github.lukehutch:fast-classpath-scanner:jar:1.2.3:compile
[INFO] | +- org.apache.httpcomponents:httpasyncclient:jar:4.1:compile
[INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.4.1:compile
[INFO] | | +- org.apache.httpcomponents:httpcore-nio:jar:4.4.1:compile
[INFO] | | +- org.apache.httpcomponents:httpclient:jar:4.4.1:compile
[INFO] | | | - commons-codec:commons-codec:jar:1.9:compile
[INFO] | | - commons-logging:commons-logging:jar:1.2:compile
[INFO] | +- javax.websocket:javax.websocket-api:jar:1.1:compile
[INFO] | +- io.undertow:undertow-core:jar:1.2.8.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.1.4.GA:compile
[INFO] | | +- org.jboss.xnio:xnio-api:jar:3.3.1.Final:compile
[INFO] | | - org.jboss.xnio:xnio-nio:jar:3.3.1.Final:runtime
[INFO] | +- io.undertow:undertow-websockets-jsr:jar:1.2.8.Final:compile
[INFO] | | +- io.undertow:undertow-servlet:jar:1.2.8.Final:compile
[INFO] | | | +- org.jboss.spec.javax.servlet:jboss-servlet-api_3.1_spec:jar:1.0.0.Final:compile
[INFO] | | | - org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] | | - org.jboss.spec.javax.websocket:jboss-websocket-api_1.1_spec:jar:1.1.0.Final:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] | - org.jsoup:jsoup:jar:1.8.2:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.5:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.5:compile
[INFO] +- org.quartz-scheduler:quartz:jar:2.2.3:compile
[INFO] | - c3p0:c3p0:jar:0.9.1.1:compile
[INFO] +- org.quartz-scheduler:quartz-jobs:jar:2.2.3:compile
这么简单的问题,logback-classic 是如何加载的,谁在加载它?
Darren Forsythe的评论就是答案。这个 -Dinclude=ch.qos.logback:logback-classic 允许我找到依赖关系。似乎依赖:树搜索只有这么深。-Dinclude 在指定依赖项的完整层次结构中搜索。