Spring定时任务中@PostConstruct被多次执行异常的分析与解决

 更新时间:2017年10月27日 09:57:54   作者:大大的微笑  
这篇文章主要给大家介绍了关于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.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容器,结果就是无限循环,异常日志太多,看不过来,异常排查的方向不对导致!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • 详解Java8 CompletableFuture的并行处理用法

    详解Java8 CompletableFuture的并行处理用法

    Java8中有一个工具非常有用,那就是CompletableFuture,本章主要讲解CompletableFuture的并行处理用法,感兴趣的小伙伴可以了解一下
    2022-04-04
  • Springboot读取外部配置文件,项目部署时配置读取不到问题及解决

    Springboot读取外部配置文件,项目部署时配置读取不到问题及解决

    这篇文章主要介绍了Springboot读取外部配置文件,项目部署时配置读取不到问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • Spring的IOC代码解析

    Spring的IOC代码解析

    这篇文章主要介绍了Spring的IOC代码解析,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • Java报错sun.misc.Unsafe.park(Native Method)问题

    Java报错sun.misc.Unsafe.park(Native Method)问题

    这篇文章主要介绍了Java报错sun.misc.Unsafe.park(Native Method)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • 一文带你深入认识JAVA中的异常

    一文带你深入认识JAVA中的异常

    Java异常处理成为社区中讨论最多的话题之一,下面这篇文章主要给大家介绍了关于JAVA中异常的相关资料,文中通过代码介绍的非常详细,对大家学习或者使用java具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-06-06
  • Java设计实现一个针对各种类型的缓存

    Java设计实现一个针对各种类型的缓存

    这篇文章主要为大家详细介绍了Java如何设计实现一个针对各种类型的缓存,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2023-11-11
  • 全面了解Java中的内部类和匿名类

    全面了解Java中的内部类和匿名类

    下面小编就为大家带来一篇全面了解Java中的内部类和匿名类。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • mybatis项目CRUD步骤实例详解

    mybatis项目CRUD步骤实例详解

    这篇文章主要介绍了mybatis项目CRUD步骤,包括pom.xml引入相应的依赖,在resources目录下写配置文件,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • Java中finalize()详解及用法

    Java中finalize()详解及用法

    这篇文章主要介绍了Java中finalize()详解及用法的相关资料,final是Java的关键字,它所表示的是“这部分是无法修改的”,需要的朋友可以参考下
    2017-03-03
  • java利用phantomjs进行截图实例教程

    java利用phantomjs进行截图实例教程

    PlantomJs是一个基于javascript的webkit内核无头浏览器 也就是没有显示界面的浏览器,你可以在基于 webkit 浏览器做的事情,它都能做到。下面这篇文章主要给大家介绍了关于java利用phantomjs进行截图的相关资料,需要的朋友可以参考下
    2018-10-10

最新评论