温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot2内嵌Tomcat抛出异常怎么解决

发布时间:2021-12-24 17:29:46 来源:亿速云 阅读:338 作者:iii 栏目:互联网科技

这篇文章主要讲解了“spring boot2内嵌Tomcat抛出异常怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“spring boot2内嵌Tomcat抛出异常怎么解决”吧!

我在使用springboot时,当代码有问题时,发现控制台打印下面信息:

Connected to the target VM, address: '127.0.0.1:42091', transport: 'socket'
log4j:WARN No appenders could be found for logger (org.springframework.boot.devtools.settings.DevToolsSettings).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2018-10-25 10:10:21.425  INFO 102158 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-10-25 10:10:21.427  INFO 102158 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-10-25 10:10:21.444  INFO 102158 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-10-25 10:10:21.590  INFO 102158 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-10-25 10:10:24.522  INFO 102158 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
Disconnected from the target VM, address: '127.0.0.1:42091', transport: 'socket'

Process finished with exit code 0

WTF?没有错误信息怎么解决问题? 各种搜索,总之就是代码有问题,自己检查把...

好吧,直接debug把

内嵌tomcat的入口类是org.apache.catalina.core.StandardService

//TODO 后面补上过程

最终找到org.springframework.context.support.AbstractApplicationContext 定位方法refresh()

if (logger.isWarnEnabled()) {
				logger.warn("Exception encountered during context initialization - " +
						"cancelling refresh attempt: " + ex);
			}

debug可以正常进入,然后就看到我们希望看到的 ex了

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fabricApiController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fabricTemplate': Injection of resource dependencies failed; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'fabricConfiguration': Could not bind properties to 'FabricConfiguration' : prefix=blockchain, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'blockchain.channel-peers' to java.util.List<com.smy.bc.fabric.core.configuration.FabricConfiguration$EndPoint>

问题发现了,解决自己代码问题,然后重新启动,正常! 万事大吉?错,这才开始

上面我们简单解决了问题,但是根源没有解决!
要说解决方案把当前流行的日志体系简单说一遍
下面整理的来源网络:

常见的日志框架,注意不是具体解决方案

1 Commons-logging: apache 最早提供的日志的门面接口。避免和具体的日志方案直接耦合。类似于JDBC的api接口,具体的的JDBC driver实现由各数据库提供商实现。通过统一接口解耦,不过其内部也实现了一些简单日志方案
2 Slf4j: 全称为Simple Logging Facade for JAVA:java简单日志门面。是对不同日志框架提供的一个门面封装。可以在部署的时候不修改任何配置即可接入一种日志实现方案。和commons-loging应该有一样的初衷。

常见的日志实现:
log4j
logback
jdk-logging

详细优缺点不是本文重点,请自行搜索。

接着分析上面的问题,Commons-logging 是tomcat默认的日志系统(apache自家东西得支持),具体的日志实现,根据系统已存在日志系统选择。 简单列举以下log的实现: org.apache.commons.logging.Log | org.apache.commons.logging.impl.SimpleLog org.apache.commons.logging.impl.NoOpLog org.apache.commons.logging.impl.Log4JLogger org.apache.commons.logging.impl.SLF4JLog org.apache.commons.logging.impl.Jdk14Logger

springboot 默认使用的是logback日志实现,问题就出现在这里了!!!common-logs并没有logback的实现!

根据maven依赖,我们看到log4j和logback的包都被引入了,然后tomcat之能选择的是log4j,springboot使用的是logback。 log4j和logback只见缺少一个桥梁,正是缺少的这个桥梁,导致springboot只能输出logback!!!

中间的桥梁就是下面这个依赖

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
    </dependency>

这个依赖可以将log4j输出到slf4j,从而从sl4j输出。

总结: 总结一下,已经搞明白是slf4j/common-logs <> log4j和logback的恩怨情仇

第一种解决方式:根据日志定位问题,然后采用加法处理,增加jcl-over-slf4j,打通slf4j和common-logs通道

第二种解决方式:解决冲突,一山不容二虎,排除掉slf4j,common-logs任意一方,spring使用slf4j,那可以排除调common-logs

感谢各位的阅读,以上就是“spring boot2内嵌Tomcat抛出异常怎么解决”的内容了,经过本文的学习后,相信大家对spring boot2内嵌Tomcat抛出异常怎么解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI