Spring定时任务中@PostConstruct被多次执行异常的分析与解决
发现问题
最近在项目中刚刚修改一个功能,代码正准备验证,启动Idea的debug模式,运行项目,发现启动失败,查看日志发现定时任务被重复执行,导致异常。debug定时任务的初始化类,发现启动定时任务是在@PostConstruct方法中执行的,网上查询,有说Spring在某种情况下初始化有bug,注解@Component可能出现多次执行。把@Component修改为@Service就行了!
结果我改了也没解决问题!
无赖更一步跟进日志,发现以下内容:
[ERROR][2017-06-2019:50:16]-[org.springframework.web.servlet.DispatcherServlet]-Contextinitializationfailed org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'requestMappingHandlerMapping'definedinclassorg.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration:Invocationofinitmethodfailed;nestedexceptionisjava.lang.IllegalStateException:Ambiguousmappingfound.Cannotmap'galleryController'beanmethod publiccom.test.model.domain.response.Resultcom.test.model.back.controller.GalleryController.removeRecord(java.lang.Integer) to{[/gallery/removeRecord],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}:Thereisalready'galleryController'beanmethod publicjava.lang.Stringcom.test.model.back.controller.GalleryController.removeRecord(java.lang.Integer,org.springframework.ui.ModelMap)mapped. atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:535)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atjavax.servlet.GenericServlet.init(GenericServlet.java:158)[servlet-api.jar:3.0.FR] atorg.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1282)[catalina.jar:7.0.70] atorg.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)[catalina.jar:7.0.70] atorg.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5349)[catalina.jar:7.0.70] atorg.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5641)[catalina.jar:7.0.70] atorg.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)[catalina.jar:7.0.70] atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)[catalina.jar:7.0.70] atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)[catalina.jar:7.0.70] atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)[catalina.jar:7.0.70] atorg.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1863)[catalina.jar:7.0.70] atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)~[na:1.7.0_79] atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)~[na:1.7.0_79] atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.7.0_79] atjava.lang.reflect.Method.invoke(Method.java:606)~[na:1.7.0_79] atorg.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)[tomcat-coyote.jar:7.0.70] atcom.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)[na:1.7.0_79] atcom.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)[na:1.7.0_79] atorg.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)[catalina.jar:7.0.70] atorg.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)[catalina.jar:7.0.70] atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)~[na:1.7.0_79] atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)~[na:1.7.0_79] atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.7.0_79] atjava.lang.reflect.Method.invoke(Method.java:606)~[na:1.7.0_79] atorg.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)[tomcat-coyote.jar:7.0.70] atcom.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)[na:1.7.0_79] atcom.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)[na:1.7.0_79] atjavax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)[na:1.7.0_79] atjavax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)[na:1.7.0_79] atjavax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)[na:1.7.0_79] atjavax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)[na:1.7.0_79] atjavax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)[na:1.7.0_79] atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)~[na:1.7.0_79] atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)~[na:1.7.0_79] atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.7.0_79] atjava.lang.reflect.Method.invoke(Method.java:606)~[na:1.7.0_79] atsun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)[na:1.7.0_79] atsun.rmi.transport.Transport$2.run(Transport.java:202)[na:1.7.0_79] atsun.rmi.transport.Transport$2.run(Transport.java:199)[na:1.7.0_79] atjava.security.AccessController.doPrivileged(NativeMethod)[na:1.7.0_79] atsun.rmi.transport.Transport.serviceCall(Transport.java:198)[na:1.7.0_79] atsun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567)[na:1.7.0_79] atsun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)[na:1.7.0_79] atsun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619)[na:1.7.0_79] atsun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684)[na:1.7.0_79] atsun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681)[na:1.7.0_79] atjava.security.AccessController.doPrivileged(NativeMethod)[na:1.7.0_79] atsun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681)[na:1.7.0_79] atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[na:1.7.0_79] atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[na:1.7.0_79] atjava.lang.Thread.run(Thread.java:745)[na:1.7.0_79] Causedby:java.lang.IllegalStateException:Ambiguousmappingfound.Cannotmap'galleryController'beanmethod publiccom.test.model.domain.response.Resultcom.test.model.back.controller.GalleryController.removeRecord(java.lang.Integer) to{[/gallery/removeRecord],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}:Thereisalready'galleryController'beanmethod publicjava.lang.Stringcom.test.model.back.controller.GalleryController.removeRecord(java.lang.Integer,org.springframework.ui.ModelMap)mapped. atorg.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:212)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:184)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:144)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:123)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126)~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] ...63commonframesomitted [2017-06-2007:50:16,467]Artifactback:warexploded:Artifactisdeployedsuccessfully [2017-06-2007:50:16,467]Artifactback:warexploded:Deploytook6,867milliseconds
解决方法:
原来,由于定时任务异常比较多,忽略了最开始的异常内容,发现程序异常后自己重新又启动了,而这个异常是GalleryController这个控制器中@RequestMapping配置重复导致的。springMVC中不允许有重复@RequestMapping,刚刚修改的方法中忘记修改@RequestMapping中的地址,所以报错,而Idea发现异常后会自动重启Tomcat容器,结果就是无限循环,异常日志太多,看不过来,异常排查的方向不对导致!
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。