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

    xiaoxiao2025-07-25  6

    首先要配置我们的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> 下面写出一个测试类: 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表达式、大家可以参考一下

    http://blog.csdn.net/u010448530/article/details/52209119

    转载请注明原文地址: https://ju.6miu.com/read-1301049.html
    最新回复(0)