Spring 解决定时任务执行两次的问题

第一种情况可能是spring扫描了两次
比如spring-config\spring-mvc都扫描了改定时任务,则更改spring-mvc配置的扫描路径过滤定时调度

<context:component-scan base-package="com.test">
        <context:exclude-filter type="annotation" expression="org.springframework.scheduling.annotation.EnableScheduling"/>
    </context:component-scan>

第二种情况可能是tomcat配置导致容器被初始化两次
由于web项目直接放在tomcat的webapp下,并且通过根路径访问,配置如下
<Context path="" docBase="..\.." debug="0" reloadable="true"/>

查找类似问题处理方式:
方法1:在conf/Catalina/localhost生成test.xml【推荐】

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
    <Context antiResourceLocking="false" privileged="true" useHttpOnly="true" path="/" docBase="..\.." >
  <!--
    Remove the comment markers from around the Valve below to limit access to
    the manager application to clients connecting from localhost
  -->
  <!--
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
  -->
</Context>

方法2:不配置根路径访问
方法3:将路径已出webapp目录,在docBase中指定路径(可以是相对于webapp的路径,或者绝对路径)
方法4:替换ROOT目录

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,058评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,977评论 6 342
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,384评论 11 349
  • 这些属性是否生效取决于对应的组件是否声明为 Spring 应用程序上下文里的 Bean(基本是自动配置的),为一个...
    发光的鱼阅读 1,454评论 0 14
  • 就像无声的影片,没有做过多的追究,仿佛一切意义只存在眼睛里,没有丝毫听取的能力。犹如一个世纪那么久,以前做的所有努...
    董落忧阅读 200评论 0 1