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引入log4j、slf4j冲突,java.…_第1张图片
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.

你可能感兴趣的:(maven引入log4j、slf4j冲突,java.…)