Expected method not foundTomcatEmbeddedContext.addApplicationListener
Expected method not foundTomcatEmbeddedContext.addApplicationListener
环境
- Spring boot:1.5.3.RELEASE
- Java:1.7
1、问题描述
Spring boot启动时报错,方法找不到异常:TomcatEmbeddedContext.addApplicationListener。详细报错信息如下:
2018-03-08 14:39:07 [main] WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext -Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.addApplicationListener(org.apache.catalina.deploy.ApplicationListener)
2018-03-08 14:39:07 [main] ERROR org.springframework.boot.SpringApplication -Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.addApplicationListener(org.apache.catalina.deploy.ApplicationListener)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.hhly.snooker.SnookerApplication.main(SnookerApplication.java:26)
Caused by: java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.addApplicationListener(org.apache.catalina.deploy.ApplicationListener)
at org.springframework.util.ClassUtils.getMethod(ClassUtils.java:641)
at org.springframework.boot.autoconfigure.websocket.TomcatWebSocketContainerCustomizer.addListener(TomcatWebSocketContainerCustomizer.java:86)
at org.springframework.boot.autoconfigure.websocket.TomcatWebSocketContainerCustomizer.access$100(TomcatWebSocketContainerCustomizer.java:37)
at org.springframework.boot.autoconfigure.websocket.TomcatWebSocketContainerCustomizer$1.customize(TomcatWebSocketContainerCustomizer.java:51)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureContext(TomcatEmbeddedServletContainerFactory.java:498)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.prepareContext(TomcatEmbeddedServletContainerFactory.java:234)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:178)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)
... 8 common frames omitted
2、问题分析
猜想有可能是因为以下原因导致的问题:
- spring boot版本
- jar冲突
首先,找到对应类,TomcatEmbeddedContext在好几个spring boot版本下均没有这个方法,说明不是spring boot版本原因导致的;
然后,通过命令查看jar依赖关系,
mvn -X compile dependency:tree -Dverbose >temp.log
然后在temp.log中查找是否有:omitted for conflict with(有就表示jar包有版本冲突),虽然编译成功了,但是还是有不少冲突。
通过排除对应的jar包,去除冲突。重新启动,依然报错;看来得去网上找找了。
后来,在网上找了一个这样的,根据报错的类名TomcatEmbeddedContext推测出,是因为项目引入了Server Runtime –> apache tomcat
导致了Spring boot内嵌的tomcat冲突导致的问题。
3、解决办法
通过排查,发现在引入的common项目中引入了Server Runtime –> apache tomcat,去掉对应依赖,项目成功启动。
囧,还有这种操作?!~