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容器,结果就是无限循环,异常日志太多,看不过来,异常排查的方向不对导致!

总结

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

相关文章

  • 通过String.intern()方法浅谈堆中常量池

    通过String.intern()方法浅谈堆中常量池

    这篇文章主要介绍了通过String.intern()方法浅谈堆中常量池,在JDK7之前,字符串常量是存在永久带Perm 区的,JDK7开始在将常量池迁移到堆中,这个变化也导致了String的新特性,下面我们慢慢进行介绍。,需要的朋友可以参考下
    2019-06-06
  • 基于JSON实现传输byte数组过程解析

    基于JSON实现传输byte数组过程解析

    这篇文章主要介绍了基于JSON实现传输byte数组过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 解决IntelliJ IDEA 控制台输出中文乱码问题(史上最简单)

    解决IntelliJ IDEA 控制台输出中文乱码问题(史上最简单)

    这篇文章主要介绍了史上最简单的IntelliJ IDEA 控制台输出中文乱码问题的解决方法,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-05-05
  • Java发送带html标签内容的邮件实例代码

    Java发送带html标签内容的邮件实例代码

    下面小编就为大家带来一篇Java发送带html标签内容的邮件实例代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Spring注解和同步锁不能同步问题解决

    Spring注解和同步锁不能同步问题解决

    这篇文章主要介绍了Spring注解和同步锁不能同步问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • 使用Java编写一个简单的Web的监控系统

    使用Java编写一个简单的Web的监控系统

    这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实用,需要的朋友可以参考下
    2015-11-11
  • java 字符串池的深入理解

    java 字符串池的深入理解

    这篇文章主要介绍了java 字符串池的深入理解的相关资料,这里提供实例代码帮助大家学习理解这部分内容,希望大家能够掌握,需要的朋友可以参考下
    2017-08-08
  • Java连接MYSQL数据库的实现步骤

    Java连接MYSQL数据库的实现步骤

    以下的文章主要描述的是java连接MYSQL数据库的正确操作步骤,在此篇文章里我们主要是以实例列举的方式来引出其具体介绍
    2013-06-06
  • spring boot tomcat jdbc pool的属性绑定

    spring boot tomcat jdbc pool的属性绑定

    这篇文章主要介绍了spring boot tomcat jdbc pool的属性绑定的相关资料,非常不错,具有参考借鉴价值,需要的朋友参考下
    2018-01-01
  • Java异常退出条件的判断示例代码

    Java异常退出条件的判断示例代码

    这篇文章主要介绍了Java异常退出条件的判断示例代码,涉及常见异常退出条件等相关内容,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10

最新评论