Spring中SLF4J多個繫結衝突問題
阿新 • • 發佈:2019-02-05
問題
Maven管理的純Spring工程中,原本是使用log4j打log的,結果依賴的庫(core-data專案)中又另外引用了logback檔案(logback與原來的log4j只能二選一),導致配置失效
以下是多個lib衝突
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/app/top-media-frag/lib/logback-classic-1.0.13.
jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/app/top-media-frag/lib/slf4j-log4j12-1.7 .5.jar
!/org/slf4j/impl/StaticLoggerBinder.class]
解決方法
方法一(暴力法)
將專案部署後刪除多餘的lib檔案
方便點使用存成shell檔案,部署指令碼自動呼叫
rm -f -v logback*
方法二(和 諧法)
在pom.xml檔案中exclusion排除衝突的包,避免core-data對主專案的影響
如下排除了關於SLF4J的包,具體在pom.xml檔案中檢視再排除
(Dependency Hierarchy中檢視)
<dependency>
<groupId>com.nickwongfree.mp</groupId >
<artifactId>core-data</artifactId>
<version>1.1</version>
<!--排除三個衝突-->
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId >logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
方法三(推鍋法)
問題的根源是依賴的庫(core-data專案)本身不規範,不應該包含具體的SLF4J的庫,讓原開發人員處理即可
另:Spring-MVC有更簡潔方式
題外話
程式設計規範呀,特別是當專案規模大起來之後,
不遵守的規範嚴重影響了效率