SpringBoot
pom
背景: springboot 1.5.9.RELEASE
我在更新log4j2版本时,在spring-boot-starter-log4j2中已经排除了 log4j-core log4j-api 2.15.0这两个关联包,并重新引入了最新包,但是启动后一直报错:出现以下异常,后来发现应该是 log4j-slf4j-impl包的问题,
- 重新引入 日志桥接包log4j-slf4j-impl 2.15.0(版本必须与log4j一致)版本,此时没有错误了,但是日志不打印了,应该还是版本不一致导致
- 进一步发现spring-boot-starter-log4j2里的关联包中 log4j-slf4j-impl的版本为 2.7, 将其也从spring-boot-starter-log4j2中排除,运行项目发现仍未打印日志
- 解决: 执行以上步骤,spring-boot-starter-log4j2中不排除log4j-slf4j-impl,直接在pom文件里设置<log4j2.version>2.15.0</log4j2.version>来直接覆盖log4j-slf4j-impl日志桥街包的版本,在外部不引入log4j-slf4j-impl 2.15.0,引入后日志仍不打印
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273)
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:179)
at org.wm.WebMonitorApplication.main(WebMonitorApplication.java:30)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.util.ReflectionUtil
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<log4j2.version>2.15.0</log4j2.version>
</properties>
————————————————————————————————————————————————————————
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
————————————————————————————————————————————————————————————
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.15.0</version>
</dependency>
spring-boot-starter-parent
- spring-boot-starter-parent相当一个父类jar包库,它为maven应用提供依赖和插件管理,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
点击进入源码可以看到以下内容:
继续点击进入可以看到管理的依赖版本:
在pom文件里加入**<**.vaersion> 1.2.3<*.version>**可以覆盖parent里管理的依赖版本。