问题描述
我是Spring Boot的新手,当我尝试启动服务器时,我会得到以下例外.我知道这与依赖性冲突有关,但仍然无法弄清楚.我正在使用Maven来管理我的依赖.请帮助
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory) Object of class [org.slf4j.impl.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext at org.springframework.util.Assert.isInstanceOf(Assert.java:339) at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:93) at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSensibleDefaults(AbstractLoggingSystem.java:62) at org.springframework.boot.logging.AbstractLoggingSystem.beforeInitialize(AbstractLoggingSystem.java:45) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:69) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:135) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98) at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100) at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:54) at org.springframework.boot.SpringApplication.run(SpringApplication.java:276) at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) at org.magnum.mobilecloud.video.Application.main(Application.java:30)
解决:将以下内容添加到pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency>
推荐答案
排除 logback-classic 来自 spring-boot-starter-web 和 spring-boot-boot-starter-starter-actuator 为我工作
compile("org.springframework.boot:spring-boot-starter-web:1.1.10.RELEASE") { exclude module: "spring-boot-starter-tomcat" exclude module: "spring-boot-starter-logging" exclude module: "logback-classic" } compile("org.springframework.boot:spring-boot-starter-actuator:1.1.10.RELEASE") { exclude module: "logback-classic" }
其他推荐答案
将其添加到您的build.gradle
configurations.all { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' exclude group: 'org.springframework.boot', module: 'logback-classic' }
其他推荐答案
在我的项目中,它正在使用
- Spring Boot 1.4.2.Release
- SLF4J 1.7.21和logback 1.1.7. (某些依赖关系称为模块A,取决于记录1.1.2,这是问题)
首先依赖机制简介 </p>
依赖性中介 - 这确定遇到多个版本的工件时将使用哪种版本的依赖关系.当前,Maven 2.0仅支持使用"最近的定义",这意味着它将使用依赖树中的最接近项目的版本.您始终可以通过在项目的POM中明确声明版本来保证版本.请注意,如果两个依赖项版本在依赖项树上的深度相同,直到Maven 2.0.8尚未定义哪一个会获胜,但是由于Maven 2.0.9,声明中的顺序是计算的:第一份声明获胜. "最近的定义"表示所使用的版本将是依赖树(例如.如果将A,B和C的依赖项定义为A-> B-> C-> D 2.0和A-> E-> D 1.0,则在构建A时使用D 1.0,因为从A到D的路径从A到D E更短.您可以在A中明确向D 2.0添加依赖项,以强迫使用D 2.0
因此,Maven将在我的项目中使用logback 1.1.7.我不确定这是我的模块与1.1.7不兼容或LogBack 1.1.7与SLF4J 1.7.21不兼容 无论如何.我在POM中添加了依赖性管理.告诉Maven仅使用锁定1.1.2.解决问题.
<dependencyManagement> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.1.2</version> </dependency> </dependencies> </dependencyManagement>
问题描述
I am new to spring boot and when I try to start my server , I get the following Exception. I understand that this has something to do with dependency conflict, but still unable to figure it out.I am using maven to manage my dependencies.Please help
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory) Object of class [org.slf4j.impl.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext at org.springframework.util.Assert.isInstanceOf(Assert.java:339) at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:93) at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSensibleDefaults(AbstractLoggingSystem.java:62) at org.springframework.boot.logging.AbstractLoggingSystem.beforeInitialize(AbstractLoggingSystem.java:45) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:69) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:135) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98) at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100) at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:54) at org.springframework.boot.SpringApplication.run(SpringApplication.java:276) at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) at org.magnum.mobilecloud.video.Application.main(Application.java:30)
Resolved:Add the following to the POM.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency>
推荐答案
Excluding logback-classic from spring-boot-starter-web and spring-boot-starter-actuator worked for me
compile("org.springframework.boot:spring-boot-starter-web:1.1.10.RELEASE") { exclude module: "spring-boot-starter-tomcat" exclude module: "spring-boot-starter-logging" exclude module: "logback-classic" } compile("org.springframework.boot:spring-boot-starter-actuator:1.1.10.RELEASE") { exclude module: "logback-classic" }
其他推荐答案
Add this to your build.gradle
configurations.all { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' exclude group: 'org.springframework.boot', module: 'logback-classic' }
其他推荐答案
In my project, it is using
- spring boot 1.4.2.RELEASE
- both slf4j 1.7.21 and logback 1.1.7. (some dependency , called module A, depend on logback 1.1.2, this is the issue)
First Introduction to the Dependency Mechanism
Dependency mediation - this determines what version of a dependency will be used when multiple versions of an artifact are encountered. Currently, Maven 2.0 only supports using the "nearest definition" which means that it will use the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins. "nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg. if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0
So maven will use logback 1.1.7 in my project. I am not sure it is my module A not compatible with 1.1.7 or logback 1.1.7 not compatible with slf4j 1.7.21 Whatever, in my case. I add dependencyManagement in my pom. Tell maven use lockback 1.1.2 only. Problem solved.
<dependencyManagement> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.1.2</version> </dependency> </dependencies> </dependencyManagement>