无法启动Spring启动服务器

时间:2014-09-26 14:23:00

标签: log4j 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>

10 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:4)

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

答案 2 :(得分:2)

我的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
}

答案 3 :(得分:1)

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

  1. spring boot 1.4.2.RELEASE
  2. slf4j 1.7.21和logback 1.1.7。 (某些依赖项,称为模块A,依赖于logback 1.1.2,这是问题)
  3. 首先Introduction to the Dependency Mechanism

      

    依赖关系中介 - 确定在遇到多个版本的工件时将使用哪个版本的依赖关系。目前,Maven 2.0仅支持使用&#34;最近的定义&#34;这意味着它将在依赖树中使用与项目最接近的依赖项的版本。您可以通过在项目的POM中明确声明版本来保证版本。请注意,如果两个依赖项版本在依赖关系树中处于相同的深度,则直到Maven 2.0.8没有定义哪一个会赢,但是从Maven 2.0.9开始,它在声明中的顺序是:第一次宣言获胜。   &#34;最近的定义&#34;意味着所使用的版本将是依赖关系树中与项目最接近的版本,例如。如果A,B和C的依赖关系被定义为A - >; B - &gt; C - &gt; D 2.0和A - &gt; E - &gt; 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中添加了dependencyManagement。告诉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>
    

答案 4 :(得分:0)

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

public void method1(string table, Expander expander)
{
    Panel panel = expander.Content as Panel;
}

答案 5 :(得分:0)

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

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

它对我有用。

答案 6 :(得分:0)

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

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

例如 enter image description here

答案 7 :(得分:0)

如果Spring Boot在类路径上,则支持Log4j 2进行日志配置。如果使用启动器来组装依赖项,则必须排除Logback,然后改为包括log4j 2。

答案 8 :(得分:0)

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

# In pydantic/main.py > BaseModel

    elif not self.__config__.allow_mutation or self.__config__.frozen:
        raise TypeError(f'"{self.__class__.__name__}" is immutable and does not support item assignment')

答案 9 :(得分:-4)

最后通过排除Logback依赖项并显式添加log4j依赖项

来解决此问题