【问题标题】:Spring Boot/Gradle/Logback: bootRun fails with "Failed to instantiate [ch.qos.logback.classic.LoggerContext]": java.lang.AbstractMethodError:Spring Boot/Gradle/Logback:bootRun 失败并显示“无法实例化 [ch.qos.logback.classic.LoggerContext]”:java.lang.AbstractMethodError:
【发布时间】:2020-02-04 05:37:46
【问题描述】:

在一个新的 Spring Boot 应用程序中,当我 gradle bootRun 时,我看到了这个错误:

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.AbstractMethodError: ch.qos.logback.classic.pattern.EnsureExceptionHandling.process(Lch/qos/logback/core/Context;Lch/qos/logback/core/pattern/Converter;)V
    at ch.qos.logback.core.pattern.PatternLayoutBase.start(PatternLayoutBase.java:86)
    at ch.qos.logback.classic.encoder.PatternLayoutEncoder.start(PatternLayoutEncoder.java:28)
    at ch.qos.logback.classic.BasicConfigurator.configure(BasicConfigurator.java:50)
    at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:164)
    at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at com.klarna.risk.decision.application.RiskDecisionApplication.<clinit>(RiskDecisionApplication.java:14)

build.gradle:

buildscript {
    repositories {
        jcenter()
        maven { url 'http://repo.spring.io/release' }
    }

    dependencies {
        classpath 'org.gretty:gretty:+'
    }
}

plugins {
    id 'org.springframework.boot' version '2.1.9.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'war'
}
apply plugin: 'org.gretty'

repositories {
    jcenter()
    maven { url 'http://repo.spring.io/release' }
}

gretty { // for Gradle 4.0+
    springBoot = true
    springBootVersion = '2.1.9.RELEASE'
    httpPort = 8080
    contextPath = ''
}

repositories {
    mavenCentral()
}
group = 'com.example.newapp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

sourceSets {
    test {
        java {
            srcDirs = ['src/test/java', 'src/api-test/java']
        }
    }
}
configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation "org.springframework.boot:spring-boot-starter-jetty"
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

有什么问题?

【问题讨论】:

    标签: java spring-boot gradle logback


    【解决方案1】:

    请尝试在您的 pom.xml 文件中添加这 3 个依赖项。它对我有用..

        <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
        </dependency>
    
        <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
        <scope>compile</scope>
        </dependency>
    
        <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.2.3</version>
        <scope>compile</scope>
        </dependency>
    

    【讨论】:

      【解决方案2】:

      当我与gradle dependencyInsight --dependency logback 核对时,我看到:

      > Task :dependencyInsight
      ch.qos.logback:logback-classic:1.1.3 (selected by rule)
         variant "compile" [
            org.gradle.status              = release (not requested)
            org.gradle.usage               = java-api
            org.gradle.libraryelements     = jar (compatible with: classes)
            org.gradle.category            = library (not requested)
      
            Requested attributes not found in the selected variant:
               org.gradle.dependency.bundling = external
               org.gradle.jvm.version         = 8
         ]
      
      ch.qos.logback:logback-classic:1.1.3
      \--- compileClasspath
      
      ch.qos.logback:logback-classic:1.2.3 -> 1.1.3
      \--- org.springframework.boot:spring-boot-starter-logging:2.1.9.RELEASE
           \--- org.springframework.boot:spring-boot-starter:2.1.9.RELEASE
                +--- compileClasspath (requested org.springframework.boot:spring-boot-starter)
                +--- org.springframework.boot:spring-boot-starter-web:2.1.9.RELEASE
                |    +--- compileClasspath (requested org.springframework.boot:spring-boot-starter-web)
                |    \--- org.springframework.boot:spring-boot-starter-websocket:2.1.9.RELEASE
                |         \--- compileClasspath
                +--- org.springframework.boot:spring-boot-starter-json:2.1.9.RELEASE
                |    +--- org.springframework.boot:spring-boot-starter-web:2.1.9.RELEASE (*)
                |    \--- org.springframework.boot:spring-boot-starter-jersey:2.1.9.RELEASE
                |         \--- compileClasspath (requested org.springframework.boot:spring-boot-starter-jersey)
                \--- org.springframework.boot:spring-boot-starter-validation:2.1.9.RELEASE
                     \--- org.springframework.boot:spring-boot-starter-jersey:2.1.9.RELEASE (*)
      
      ch.qos.logback:logback-core:1.2.3 (selected by rule)
         variant "compile" [
            org.gradle.status              = release (not requested)
            org.gradle.usage               = java-api
            org.gradle.libraryelements     = jar (compatible with: classes)
            org.gradle.category            = library (not requested)
      
            Requested attributes not found in the selected variant:
               org.gradle.dependency.bundling = external
               org.gradle.jvm.version         = 8
         ]
      
      ch.qos.logback:logback-core:1.1.3 -> 1.2.3
      \--- ch.qos.logback:logback-classic:1.1.3
           +--- compileClasspath
           \--- org.springframework.boot:spring-boot-starter-logging:2.1.9.RELEASE (requested ch.qos.logback:logback-classic:1.2.3)
                \--- org.springframework.boot:spring-boot-starter:2.1.9.RELEASE
                     +--- compileClasspath (requested org.springframework.boot:spring-boot-starter)
                     +--- org.springframework.boot:spring-boot-starter-web:2.1.9.RELEASE
                     |    +--- compileClasspath (requested org.springframework.boot:spring-boot-starter-web)
                     |    \--- org.springframework.boot:spring-boot-starter-websocket:2.1.9.RELEASE
                     |         \--- compileClasspath
                     +--- org.springframework.boot:spring-boot-starter-json:2.1.9.RELEASE
                     |    +--- org.springframework.boot:spring-boot-starter-web:2.1.9.RELEASE (*)
                     |    \--- org.springframework.boot:spring-boot-starter-jersey:2.1.9.RELEASE
                     |         \--- compileClasspath (requested org.springframework.boot:spring-boot-starter-jersey)
                     \--- org.springframework.boot:spring-boot-starter-validation:2.1.9.RELEASE
                          \--- org.springframework.boot:spring-boot-starter-jersey:2.1.9.RELEASE (*)
      
      (*) - dependencies omitted (listed previously)
      
      A web-based, searchable dependency report is available by adding the --scan option.
      

      似乎 Spring Boot 2.1.9 使用的是org.springframework.boot:spring-boot-starter-logging:2.1.9.RELEASE,它使用的是logback-classic:1.1.3。 Google 的某处告诉我 Spring Boot 依赖管理有一个错误,它允许 logback-corelogback-classic 的不同版本在这里并导致冲突。

      我添加了logback-core:1.1.3的依赖,问题就解决了。

      compile group: 'ch.qos.logback', name: 'logback-core', version: '1.1.3'
      

      如果你遇到同样的问题,请务必检查 Spring Boot 中使用的 logback 版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-19
        • 2018-03-11
        • 1970-01-01
        • 2018-10-22
        • 1970-01-01
        • 2020-05-09
        • 2019-01-01
        相关资源
        最近更新 更多