背景
SpringBoot项目在启动时报 Exception in thread "main" java.lang.StackOverflowError。
问题排查
作为一个夜夜仰望星空的码农这个异常再熟悉不过了-"堆栈溢出",
Exception in thread "main" java.lang.StackOverflowError
at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:108)
at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:121)
at org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:39)
at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:52)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
后面是重复日志,就不贴出来了
如果是个新手到这里可能就懵B啦,完全跟应用代码无关,全是log4j的日志,如何解决,而作为填坑老手的你肯定已经默默进入showTime时间,话不多说,这里造成堆栈溢出的原因就是log的Jar包冲突
,使用命令
mvn dependency:tree
打印出jar包依赖树,或者使用idea的可视化工具show Dependencies 查看jar包依赖关系,我这里同时有log4j-slf4j-impl 和log4j-to-slf4j两种实现,去掉一种即可,其中log4j-to-slf4j是springboot2 自带,取消即可,代码如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-to-slf4j</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
重新启动应用,至此我们的应用又可以清清爽爽的启动起来啦。