创建新的 Spring Boot 项目时出现 SLF4J 问题

SLF4J issues when creating a new SpringBoot project

我知道有很多 post 谈论 SLF4J 问题,但其中 none 帮助我解决了我的问题。

我用 Spring Initializer 创建了一个新项目,依赖项 mongoDBwebrest,并将导入的文件作为模块下载到新的 IntelliJ 项目中。

我可以做到 mvn clean install 并且有效。但这是我 运行 主 class 时收到的错误消息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Applications/IntelliJ%20IDEA.app/Contents/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/matteweon/.m2/repository/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

这是我的 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>poc.local</groupId>
    <artifactId>robot-shop-store</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>robot-shop-store</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <!--exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jcl-over-slf4j</artifactId>
                </exclusion>
            </exclusions-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <!--exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jcl-over-slf4j</artifactId>
                </exclusion>
            </exclusions-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我尝试在 mongodb 和 data-rest 依赖项下向 slf4j 添加一些排除项(您可以在该文件中查看注释),错误更改为另一个:

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 loaded from file:/Applications/IntelliJ%20IDEA.app/Contents/lib/slf4j-log4j12-1.7.10.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.Log4jLoggerFactory

有什么想法吗?

当我尝试 运行 另一个项目时,我已经完成了相同的依赖关系并且它工作得很好,所以我知道导致问题的不是项目,而是 IntelliJ 本身。所以我删除了 IntelliJ 上的模块和 IntelliJ 的配置文件(.iml 文件和 .idea 文件夹),我重新导入了项目,现在它正在运行

我对我的 pom.xml 文件做了很多更改,例如排除 slf4j,但 worked.Finally 没有通过删除 slf4j-log4j12-1.7 解决问题。10.jar.This 应该可以解决您的问题。

Jar Path: file:/Applications/IntelliJ%20IDEA.app/Contents/lib/slf4j-log4j12-1.7.10.jar