本篇介绍一个实际开发中遇到的问题——关于循环依赖的踩坑;
现象:启动工程时,偶尔成功,偶尔失败;
过程:看到报错的位置是catalina开头的,以为是tomcat的环境问题,因为是偶现,所以没有仔细查看报错的日志;认为从测试环境调整为生产环境问题大概率就能解决,结果生产环境的部署刚好失败了;
部署日志:关键的日志及错误信息已经标记出,提示为:
Jun 11, 2020 3:57:01 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jun 11, 2020 3:57:01 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 11, 2020 3:57:01 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 367 ms
Jun 11, 2020 3:57:01 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 11, 2020 3:57:01 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/x.x.90
Jun 11, 2020 3:57:04 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1241)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:819)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1702)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1692)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
... 6 more
Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies.
The class hierarchy being processed was [org.bouncycastle.asn1.ASN1Boolean->org.bouncycastle.asn1.DERBoolean->org.bouncycastle.asn1.ASN1Boolean]
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2164)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2110)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1971)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1932)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1917)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1322)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:388)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5566)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
... 6 more
解释:
"Unable to complete the scan for annotations for web application [] due to a StackOverflowError."
——扫描注解失败,原因是堆栈溢出StackOverflowError;
"Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies."
——可能的根本原因包括-Xss设置过低和非法的循环继承依赖关系;
其实原因已经在日志中打出来了,循环引用导致的堆栈溢出;
因此,这个问题的根本原因是jar包冲突,继承关系刚好相反,故造成tomcat启动的循环依赖问题,导致堆栈溢出,处理问题的思路应该是如何解决jar依赖问题;
从org.bouncycastle.asn1.ASN1Boolean这个类入手。查看系统中 ASN1Boolean 出现在了两个包中:
解决方案:
排除其中一个依赖即可,可以安装IDEA Maven Helper插件(强烈推荐),搜索这个org.bouncycastle,然后右键exclude即可;注意,直接去父pom中搜不到,需要在子module的pom文件中搜索,排除依赖可以放在父pom中;
org.bouncycastle
bcprov-jdk16
故在pom中排除相应jar包,重新构建,部署成功!
思考:编码中遇到的任何异常情况,哪怕是偶现的,也需要重视;需要寻根溯源,提前识别到问题并尽可能在生产环境部署前解决;部署日志提供了排查问题的信息,需要仔细查看,而不是看到某一段异常信息就妄下结论!
补充下Spring解决循环依赖的方式:《Spring——解决循环依赖》
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)