我有一个弹簧启动应用程序,其中logback与sl4j一起使用。它完美地记录了所有内容,但如果在服务启动时出现一些错误,例如BeanInitializationException,那么控制台中不会出现这种情况。
我检查了依赖树,然后发现 log4j jar 也存在,所以我排除了它,认为问题可能是由于 logback 和 log4j 之间的冲突。但是现在一些第三方jar要求log4j。
<dependency>
<groupId>org.owasp.esapi</groupId>
<artifactId>esapi</artifactId>
<version>${org.owasp.esapi.version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
我收到此错误
20-08-2019 12:39:46.117 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext.log - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'connectorController': Unsatisfied dependency expressed through field 'connectorService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'connectorService': Unsatisfied dependency expressed through field 'concurConnector'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getConcurConnector' defined in class path resource [com/oversighttech/application/config/ConnectorConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.oversighttech.Concur.ConcurConnector]: Factory method 'getConcurConnector' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Logger
尝试使用log4j-over-slf4j
依赖项。它是 log4j API 的替代品,并将调用委托给 slf4j 进行日志记录。
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.28</version>
</dependency>
https://www.slf4j.org/legacy.html#log4j-over-slf4j