SpringMVC框架使用注解执行定时任务

首先要配置我们的SpringMVC文件

    xmlns 加下面的内容、

xmlns:task="http://www.springframework.org/schema/task"

    然后xsi:schemaLocation加下面的内容、

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd

    最后是我们的task任务扫描注解

<!-- task任务扫描注解 -->
<task:annotation-driven/>

    我配置的扫描位置是

<context:component-scan base-package="com.wuzhut"></context:component-scan><code class="hljs tcl">
</code>
<span style="font-size:24px;">下面写出一个测试类</span>
package com.wuzhut.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyTask {
    @Scheduled(cron="0/5 * * * * ? ") //间隔5秒执行
    public void taskCycle(){
        System.out.println("无主题(www.wuzhuti.cn) <span style="color: #000000;">专注于前端开发技术和<span id="3_nwp" style="width: auto; height: auto; float: none;"><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=d1b169b675029816&k=%B3%CC%D0%F2%BF%AA%B7%A2&k0=%B3%CC%D0%F2%BF%AA%B7%A2&kdi0=0&luki=4&n=10&p=baidu&q=06003100_cpr&rb=0&rs=1&seller_id=1&sid=16980275b669b1d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1948625&u=http%3A%2F%2Fwuzhuti%2Ecn%2F850%2Ehtml&urlid=0" target="_blank" mpid="3" style="text-decoration: none;"><span style="color:#0000ff;font-size:12px;width:auto;height:auto;float:none;">程序开发</span></a></span>研究的技术博客</span>");
    }
}

注意

需要注意的几点:

1、spring的@Scheduled注解  需要写在实现上、

2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)

3、实现类上要有组件的注解@Component

4、下面的文章链接是corn表达式、大家可以参考一下



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值