Spring定时任务中@PostConstruct被多次执行异常的分析与解决
程序员文章站
2024-02-19 12:02:34
发现问题
最近在项目中刚刚修改一个功能,代码正准备验证,启动idea的debug模式,运行项目,发现启动失败,查看日志发现定时任务被重复执行,导致异常。debug定时任务...
发现问题
最近在项目中刚刚修改一个功能,代码正准备验证,启动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.web.servlet.config.annotation.delegatingwebmvcconfiguration: invocation of init method failed; nested exception is java.lang.illegalstateexception: ambiguous mapping found. cannot map 'gallerycontroller' bean method public com.test.model.domain.response.result com.test.model.back.controller.gallerycontroller.removerecord(java.lang.integer) to {[/gallery/removerecord],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: there is already 'gallerycontroller' bean method public java.lang.string com.test.model.back.controller.gallerycontroller.removerecord(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.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) ~[spring-beans-4.1.6.release.jar:4.1.6.release] at org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:303) ~[spring-beans-4.1.6.release.jar:4.1.6.release] at org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(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.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) ~[spring-beans-4.1.6.release.jar:4.1.6.release] at org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:755) ~[spring-beans-4.1.6.release.jar:4.1.6.release] at org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:757) ~[spring-context-4.1.6.release.jar:4.1.6.release] at org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:480) ~[spring-context-4.1.6.release.jar:4.1.6.release] at org.springframework.web.servlet.frameworkservlet.configureandrefreshwebapplicationcontext(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.apache.catalina.core.standardwrapper.initservlet(standardwrapper.java:1282) [catalina.jar:7.0.70] at org.apache.catalina.core.standardwrapper.load(standardwrapper.java:1088) [catalina.jar:7.0.70] at org.apache.catalina.core.standardcontext.loadonstartup(standardcontext.java:5349) [catalina.jar:7.0.70] at org.apache.catalina.core.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.apache.catalina.core.containerbase.addchildinternal(containerbase.java:899) [catalina.jar:7.0.70] at org.apache.catalina.core.containerbase.addchild(containerbase.java:875) [catalina.jar:7.0.70] at org.apache.catalina.core.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] at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.7.0_79] at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) ~[na:1.7.0_79] at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:1.7.0_79] at java.lang.reflect.method.invoke(method.java:606) ~[na:1.7.0_79] at org.apache.tomcat.util.modeler.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.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:618) [catalina.jar:7.0.70] at org.apache.catalina.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:565) [catalina.jar:7.0.70] at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.7.0_79] at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) ~[na:1.7.0_79] at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:1.7.0_79] at java.lang.reflect.method.invoke(method.java:606) ~[na:1.7.0_79] at org.apache.tomcat.util.modeler.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 javax.management.remote.rmi.rmiconnectionimpl.dooperation(rmiconnectionimpl.java:1487) [na:1.7.0_79] at javax.management.remote.rmi.rmiconnectionimpl.access$300(rmiconnectionimpl.java:97) [na:1.7.0_79] at javax.management.remote.rmi.rmiconnectionimpl$privilegedoperation.run(rmiconnectionimpl.java:1328) [na:1.7.0_79] at javax.management.remote.rmi.rmiconnectionimpl.doprivilegedoperation(rmiconnectionimpl.java:1420) [na:1.7.0_79] at javax.management.remote.rmi.rmiconnectionimpl.invoke(rmiconnectionimpl.java:848) [na:1.7.0_79] at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.7.0_79] at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) ~[na:1.7.0_79] at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:1.7.0_79] at java.lang.reflect.method.invoke(method.java:606) ~[na:1.7.0_79] at sun.rmi.server.unicastserverref.dispatch(unicastserverref.java:322) [na:1.7.0_79] at sun.rmi.transport.transport$2.run(transport.java:202) [na:1.7.0_79] at sun.rmi.transport.transport$2.run(transport.java:199) [na:1.7.0_79] at java.security.accesscontroller.doprivileged(native method) [na:1.7.0_79] at sun.rmi.transport.transport.servicecall(transport.java:198) [na:1.7.0_79] at sun.rmi.transport.tcp.tcptransport.handlemessages(tcptransport.java:567) [na:1.7.0_79] at sun.rmi.transport.tcp.tcptransport$connectionhandler.run0(tcptransport.java:828) [na:1.7.0_79] at sun.rmi.transport.tcp.tcptransport$connectionhandler.access$400(tcptransport.java:619) [na:1.7.0_79] at sun.rmi.transport.tcp.tcptransport$connectionhandler$1.run(tcptransport.java:684) [na:1.7.0_79] at sun.rmi.transport.tcp.tcptransport$connectionhandler$1.run(tcptransport.java:681) [na:1.7.0_79] at java.security.accesscontroller.doprivileged(native method) [na:1.7.0_79] at sun.rmi.transport.tcp.tcptransport$connectionhandler.run(tcptransport.java:681) [na:1.7.0_79] at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) [na:1.7.0_79] at java.util.concurrent.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 public com.test.model.domain.response.result com.test.model.back.controller.gallerycontroller.removerecord(java.lang.integer) to {[/gallery/removerecord],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: there is already 'gallerycontroller' bean method public java.lang.string com.test.model.back.controller.gallerycontroller.removerecord(java.lang.integer,org.springframework.ui.modelmap) mapped. at org.springframework.web.servlet.handler.abstracthandlermethodmapping.registerhandlermethod(abstracthandlermethodmapping.java:212) ~[spring-webmvc-4.1.6.release.jar:4.1.6.release] at org.springframework.web.servlet.handler.abstracthandlermethodmapping.detecthandlermethods(abstracthandlermethodmapping.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.servlet.mvc.method.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(abstractautowirecapablebeanfactory.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容器,结果就是无限循环,异常日志太多,看不过来,异常排查的方向不对导致!
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。