1. ホーム
  2. Other

SprinBootプロジェクト起動エラー スレッド "main "で例外 java.lang.reflect.InvocationTargetException

2022-02-14 10:54:45
<パス

エラーコードです。

lottery-api-start ........
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/data/javaprogram/lottery_api/lottery_api.war!/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/ StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/data/javaprogram/lottery_api/lottery_api.war!/WEB-INF/lib/logback-classic-1.2.3.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]
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:58)
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory loaded from jar:file:/data/javaprogram/lottery_api/lottery_api. war!/WEB-INF/lib/slf4j-log4j12-1.7.25.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
        at org.springframework.util.Assert.instanceCheckFailed(Assert.java:637)
        at org.springframework.util.Assert.isInstanceOf(Assert.java:537)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:274)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:99)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java: 191)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:170)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
        at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:68)
        at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
        at org.springframework.boot.SpringApplication.run(Spr
        ... 8 more


理由

クラスパスを見てみると、SLF4J のロギングインターフェイスが 2 回バインドされていることがわかります。
ログの公式ドキュメントを確認した結果

The SLF4J API is designed to bind with one and only one underlying logging framework at a time. If more than one binding is present on the class path, SLF4J will emit a warning, listing the location of those bindings.

When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. have both slf4j-simple-1.8.0-beta4.jar and slf4j-nop-1.8.0-beta4.jar on the class path and you wish to use the nop (no-operation) binding, then remove slf4j-simple-1.8.0-beta4.jar from the class path.

The list of locations that SLF4J provides in this warning usually provides sufficient information to identify the dependency transitively pulling in an In your project's pom.xml file, exclude this SLF4J binding when declaring the unscrupulous dependency. For example, cassandra-all version 0.8.1 declares both log4j and slf4j-log4j12 as compile-time dependencies. Thus, when you include cassandra-all as a dependency in your project, the cassandra-all declaration will cause both slf4j-log4j12.jar and log4j.jar to be pulled in as dependencies. do not wish to use log4j as the the SLF4J backend, you can instruct Maven to exclude these two artifacts as shown next:

<dependencies>
  <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

    <exclusions>
      <exclusion> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions> 

  </dependency>
</dependencies>
Even when multiple bindings are present, SLF4J will pick one logging framework/implementation and bind with it. The way SLF4J picks a binding is determined by the JVM and for all practical purposes should be considered random, SLF4J will name the framework/implementation class it is actually bound to.

Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J's purpose. When you come across an embedded component declaring a compile-time dependency on any SLF4J binding, please take the time to contact the authors of said component/library and kindly ask them to mend their ways.



参照依存関係が与えられていますが、私が参照する外部jarはspringboot自身のslf4jと衝突し、依存関係は私の問題を解決してくれません。

そこで、最終的にどちらかのjarを削除するか、springboot自身のslf4jを削除することで解決します。

jarは再度コンパイルしてパッケージングする必要があり、面倒なので、sb自体からslf4jを削除することにしました。
私なりの解決策

自分のプロジェクトのパブリックアプリケーションのJARパッケージで、すでにorg.slf4jを参照しているものがあるので、SpringBoot自身が運ぶ「org.slf4j」を除外したいのですが、どうしたらいいですか?

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions> 
            <! -- 
            <exclusion> 
                <groupId>ch.qos.logback</groupId> 
                <artifactId>logback-classic</artifactId> 
            </exclusion>
             -->
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion> 
        </exclusions>
    </dependency>


ネットで見つけた解決策

解決策1

starter-web はスターターの依存関係から slf4j を除外します。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
   </exclusions>


解決策2

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>


起動は、jarのデフォルトのログレベルで開始されます。

すると、無事に起動しました

SLF4Jのドキュメントです。 http://www.slf4j.org/codes.html#multiple_bindings

springbootのログバックログを削除し、流し込んだ外部jarと同期するログに置き換えることをお勧めします。

解決策3

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


解決策4

<dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </dependency>



by おばちゃんのケチャップ
出典 CSDN
元の記事 https://blog.csdn.net/weixin_38361347/article/details/88351246
著作権について これはオリジナルのブログ記事です。ブログ記事へのリンクを貼ってください