码迷,mamicode.com
首页 > 编程语言 > 详细

spring-task解决定时问题

时间:2017-06-24 10:01:50      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:问题   port   bean   blog   mvc   word   spring定时   one   version   

 

 spring3以上版本,spring-content自带 spring-task ,来解决工程中的定时问题

 基于注解配置spring定时任务

spring配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd ">

    <mvc:annotation-driven />

    <context:component-scan base-package="com" />
    
     <!--开启该组件就可以,基于注解使用定时任务啦 -->
    <task:annotation-driven />

</beans>

java代码中的书写规范:

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

@Service
public class Demo
{
    @Scheduled(cron = "*/5 * * * * ?") 
    public void tast()
    {
        System.out.println("定时任务");
    }
}

 

spring-task解决定时问题

标签:问题   port   bean   blog   mvc   word   spring定时   one   version   

原文地址:http://www.cnblogs.com/wangfeixiong/p/7072125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!