首页 > 编程语言 > 详细

spring时间管理

时间:2017-08-07 18:06:23      阅读:226      评论:0      收藏:0      [点我收藏+]

spring时间管理相比Quartz要简单的多,但功能不如quartz强大

spring.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
">

    <context:component-scan base-package="task"/>

</beans>

要执行的任务

package task;

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

/**
 * Created by MY on 2017/8/7.
 */
@Service
public class TaskService {
    //每三秒执行一次
    @Scheduled(cron ="0/3 * 17 * * ?")
    public void print(){
        System.out.println("spring任务");
    }
}

启用任务调动

package task;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * Created by MY on 2017/8/7.
 */
@Configuration
@EnableScheduling
//启用任务调动
public class TaskConfig {
}

读取spring.xml文件即可,无需做其他操作

package task;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by MY on 2017/8/7.
 */
public class SpringTask {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext cxt=new ClassPathXmlApplicationContext("spring.xml");
    }
}

执行结果

spring任务
spring任务
spring任务
spring任务

 

spring时间管理

原文:http://www.cnblogs.com/rzqz/p/7300487.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!