1. 程式人生 > >maven引入log4j、slf4j衝突,java.…

maven引入log4j、slf4j衝突,java.…

tomcat啟動報錯:java.lang.LinkageError:loader constraint violation: when resolving method"org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;"the class loader (instance oforg/apache/catalina/loader/WebappClassLoader) of the current class,org/slf4j/LoggerFactory, and the class loader (instance of forresolved class, org/slf4j/impl/StaticLoggerBinder, have differentClass objects for the type LoggerFactory; used in thesignature
原因是tomcat自帶的lib下已經有了log4j、slf4j的jar包,修改專案這幾個依賴的scope為provided即可。 maven依賴的scope預設是compile compile表示被依賴專案需要參與當前專案的編譯,參與test、runtime,是一個比較強的依賴。而修改為provided意味著打包的時候可以不用包進去,執行時JDK或容器(如tomcat)會提供。 scope的官方文件解釋如下: scope:
This element refers to the classpath of the task at hand (compilingand runtime, testing, etc.) as well as how to limit thetransitivity of a dependency. There are five scopesavailable:
  • compile - this is the default scope,used if none is specified. Compile dependencies are available inall classpaths. Furthermore, those dependencies are propagated todependent projects.
  • provided - this is much like compile,but indicates you expect the JDK or a container to provide it atruntime. It is only available on the compilation and testclasspath, and is not transitive.
  • runtime - this scope indicates that thedependency is not required for compilation, but is for execution.It is in the runtime and test classpaths, but not the compileclasspath.
  • test - this scope indicates that thedependency is not required for normal use of the application, andis only available for the test compilation and execution phases. Itis not transitive.
  • system - this scope is similarto provided exceptthat you have to provide the JAR which contains it explicitly. Theartifact is always available and is not looked up in arepository.