zoukankan      html  css  js  c++  java
  • 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>

    下面写出一个测试类:

    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=" 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;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

  • 相关阅读:
    python 学习记录
    Oracle Spatial构建自定义投影坐标系
    GeoWebCache参数之pixelSize(像素大小)
    MapViewer提示错误之Cannot read USER_SDO_TILE_ADMIN_TASKS view from database
    《微软ASP.NET和AJAX:构建网络应用程序》
    Microsoft Dynamics CRM 2011 beta 今天发布!
    重要:欢迎光临新版微软中文CRM论坛!
    快讯:CRM Update Rollup(升级包) 每8周一个!
    微软的云策略
    风向标:Microsoft Dynamics 与企业生产力
  • 原文地址:https://www.cnblogs.com/ruiati/p/6932344.html
Copyright © 2011-2022 走看看