【问题标题】:Unable to start spring boot server无法启动spring boot服务器
【发布时间】:2014-09-26 14:23:00
【问题描述】:

我是 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>

【问题讨论】:

  • 你能提供你的pom.xml吗?
  • 您好,终于通过排除Logback依赖并显式添加log4j依赖解决了这个问题。
  • 很高兴听到你想通了。回答您自己的问题没有害处:您应该将您的解决方案作为答案发布并接受它。我对 Spring Boot 进行了改进,以便它能够识别竞争 LoggerFactory 实现的来源,并希望使问题更容易诊断:github.com/spring-projects/spring-boot/commit/…
  • 安迪,gr8...谢谢你
  • 我知道这是一个老问题,但为了未来的读者:记住“mvn dependency:tree”任务是你的朋友。它需要一些工作——例如,你必须认识到 org.slf4j:slf4j-simple 与 logback 冲突——但是当面临这个问题时(通常是因为传递依赖),以树形式查看依赖通常会让我走上通往解决方案。

标签: log4j spring-boot


【解决方案1】:

从 spring-boot-starter-web 和 spring-boot-starter-actuator 中排除 logback-classic > 为我工作

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"
}

【讨论】:

  • 我可以确认它有帮助。
【解决方案2】:

将此添加到您的 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'
}

【讨论】:

    【解决方案3】:

    在我的项目中,它正在使用

    1. spring boot 1.4.2.RELEASE
    2. slf4j 1.7.21 和 logback 1.1.7。 (一些依赖,称为模块 A,依赖于 logback 1.1.2,这就是问题所在)

    第一Introduction to the Dependency Mechanism

    依赖中介——这决定了当遇到工件的多个版本时将使用哪个版本的依赖。目前,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 的路径通过E 更短。您可以在 A 中显式添加对 D 2.0 的依赖项以强制使用 D 2.0

    所以 maven 将在我的项目中使用 logback 1.1.7。我不确定是我的模块 A 与 1.1.7 不兼容还是 logback 1.1.7 与 slf4j 1.7.21 不兼容 无论如何,就我而言。我在我的 pom.xml 中添加了dependencyManagement。告诉 maven 只使用 lockback 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>
    

    【讨论】:

    • 谢谢你的朋友,其他解决方案没有用,但明确定义了!
    【解决方案4】:

    我的 gradle.build 文件中的以下配置对我有用:

    configurations {
        all*.exclude group: "org.springframework.boot", module: "spring-boot-starter-logging"
        all*.exclude group: "ch.qos.logback"
        all*.exclude group: "org.slf4j", module: "log4j-over-slf4j" // allow using log4j 2.x
        all*.exclude group: "org.slf4j", module: "slf4j-simple"     // log4j is the configured backend
    }
    

    【讨论】:

      【解决方案5】:

      重复回答,但您可以使用 Eclipse 排除 spring-boot-starter-logging 依赖项

      1. 选择依赖层次结构
      2. 右上角搜索日志
      3. 选择 spring-boot-starter-logging
      4. 右键排除 maven 工件

      例如 enter image description here

      【讨论】:

        【解决方案6】:

        Gradle 解决方案正在 build.gradle 中添加以下行:

        configurations {
            all*.exclude module : 'spring-boot-starter-logging'
        }
        

        【讨论】:

          【解决方案7】:

          我建议你尝试删除任何包含 Logback 的依赖项,最常见的是:

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-log4j2</artifactId>
          </dependency>
          

          它对我有用。

          【讨论】:

            【解决方案8】:

            Spring Boot 支持 Log4j 2 用于日志记录配置(如果它位于类路径中)。如果您使用启动器来组装依赖项,则必须排除 Logback,然后改为包含 log4j 2。

            【讨论】:

            • 对我有用的 Gradle 配置与上面提到的相同。配置 { all*.exclude 组:“org.springframework.boot”,模块:“spring-boot-starter-logging” all*.exclude 组:“ch.qos.logback” all*.exclude 组:“org.slf4j ", module: "log4j-over-slf4j" // 允许使用 log4j 2.x all*.exclude group: "org.slf4j", module: "slf4j-simple" // log4j 是配置的后端 }
            【解决方案9】:

            通过按此顺序添加以下依赖项解决了该问题

             <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-log4j2</artifactId>
                </dependency>
            
             <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>
            

            【讨论】:

              【解决方案10】:

              最后通过排除Logback依赖并显式添加log4j依赖解决了这个问题

              【讨论】:

              • 你能分享一下gradle的变化吗?
              猜你喜欢
              • 2019-10-11
              • 1970-01-01
              • 2021-05-25
              • 2019-12-20
              • 1970-01-01
              • 2020-11-05
              • 2020-04-03
              • 1970-01-01
              • 2015-03-30
              相关资源
              最近更新 更多