Spring定时任务中@PostConstruct被多次执⾏异常的分析与解决
发现问题
最近在项⽬中刚刚修改⼀个功能,代码正准备验证,启动Idea的debug模式,运⾏项⽬,发现启动失败,查看⽇志发现定时任务被重复执⾏,导致异常。debug定时任务的初始化类,发现启动定时任务是
在@PostConstruct⽅法中执⾏的,⽹上查询,有说Spring在某种情况下初始化有bug,注解@Component可能出现多次执⾏。把@Component修改为@Service就⾏了!
结果我改了也没解决问题!
⽆赖更⼀步跟进⽇志,发现以下内容:
[ERROR][2017-06-20 19:50:16] - [org.springframework.web.servlet.DispatcherServlet] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.fig.annotation.DelegatingWebMvcConfiguration: Invocation of init m
ethod failed; ne st.sponse.st.veRecord(java.lang.Integer)
to {[/gallery/removeRecord],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'galleryController' bean method
public java.lang.st.veRecord(java.lang.Integer,org.springframework.ui.ModelMap) mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.ateBean(Abstr
actAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.Object(AbstractBeanFactory.java:303) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(Def
aultListableBeanFactory.java:755) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at t.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at t.fresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.figureAndRefreshWebApplicationContext(FrameworkServlet.java:663) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:535) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at javax.servlet.GenericServlet.init(GenericServlet.java:158) [servlet-api.jar:3.0.FR]
at org.StandardWrapper.initServlet(StandardWrapper.java:1282) [catalina.jar:7.0.70]
at org.StandardWrapper.load(StandardWrapper.java:1088) [catalina.jar:7.0.70]
at org.StandardContext.loadOnStartup(StandardContext.java:5349) [catalina.jar:7.0.70]
at org.StandardContext.startInternal(StandardContext.java:5641) [catalina.jar:7.0.70]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:7.0.70]
at org.ContainerBase.addChildInternal(ContainerBase.java:899) [catalina.jar:7.0.70]
at org.ContainerBase.addChild(ContainerBase.java:875) [catalina.jar:7.0.70]
at org.StandardHost.addChild(StandardHost.java:652) [catalina.jar:7.0.70]
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1863) [catalina.jar:7.0.70]
flect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
at flect.Method.invoke(Method.java:606) ~[na:1.7.0_79]
at deler.BaseModelMBean.invoke(BaseModelMBean.java:301) [tomcat-coyote.jar:7.0.70]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [na:1.7.0_79]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [na:1.7.0_79]
at org.apache.catalina.ateStandardContext(MBeanFactory.java:618) [catalina.jar:7.0.70]
at org.apache.catalina.ateStandardContext(MBeanFactory.java:565) [catalina.jar:7.0.70]
flect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]
springframework事务flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
at flect.Method.invoke(Method.java:606) ~[na:1.7.0_79]
at deler.BaseModelMBean.invoke(BaseModelMBean.java:301) [tomcat-coyote.jar:7.0.70]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [na:1.7.0_79]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [na:1.7.0_79]
at i.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487) [na:1.7.0_79]
at i.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97) [na:1.7.0_79]
at i.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328) [na:1.7.0_79]
at i.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420) [na:1.7.0_79]
at i.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848) [na:1.7.0_79]
flect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
at flect.Method.invoke(Method.java:606) ~[na:1.7.0_79]
i.server.UnicastServerRef.dispatch(UnicastServerRef.java:322) [na:1.7.0_79]
i.transport.Transport$2.run(Transport.java:202) [na:1.7.0_79]
i.transport.Transport$2.run(Transport.java:199) [na:1.7.0_79]
at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_79]
i.transport.Transport.serviceCall(Transport.java:198) [na:1.7.0_79]
p.TCPTransport.handleMessages(TCPTransport.java:567) [na:1.7.0_79]
p.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828) [na:1.7.0_79]
p.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619) [na:1.7.0_79]
p.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684) [na:1.7.0_79]
p.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681) [na:1.7.0_79]
at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_79]
p.TCPTransport$ConnectionHandler.run(TCPTransport.java:681) [na:1.7.0_79]
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_79]
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_79]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_79]
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'galleryController' bean method
st.sponse.st.veRecord(java.lang.Integer)
to {[/gallery/removeRecord],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'galleryController' bean method
public java.lang.st.veRecord(java.lang.Integer,org.springframework.ui.ModelMap) mapped.
at org.springframework.web.servlet.isterHandlerMethod(AbstractHandlerMethodMapping.java:212) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(A
bstractHandlerMethodMapping.java:184) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:144) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:123) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.hod.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(Abs
tractAutowireCapableBeanFactory.java:1570) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
... 63 common frames omitted
[2017-06-20 07:50:16,467] Artifact back:war exploded: Artifact is deployed successfully
[2017-06-20 07:50:16,467] Artifact back:war exploded: Deploy took 6,867 milliseconds
解决⽅法:
原来,由于定时任务异常⽐较多,忽略了最开始的异常内容,发现程序异常后⾃⼰重新⼜启动了,⽽这个异常是GalleryController这个控制器中@RequestMapping配置重复导致的。springMVC中不允许
有重复@RequestMapping,刚刚修改的⽅法中忘记修改@RequestMapping中的地址,所以报错,⽽Idea发现异常后会⾃动重启Tomcat容器,结果就是⽆限循环,异常⽇志太多,看不过来,异常排查的
⽅向不对导致!
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,如
果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论