设为首页 加入收藏

TOP

spring自带定时器@Scheduled注解配置
2017-10-12 11:32:12 】 浏览:2930
Tags:spring 自带 定时器 @Scheduled 注解 配置

1.仅需引入spring相关的包。

2.在xml里加入task的命名空间

xmlns:task="http://www.springframework.org/schema/task"   
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd 

3.配置定时任务的线程池

<task:annotation-driven scheduler="myScheduler" mode="proxy" />  
<task:scheduler id="myScheduler" pool-size="10"/> 

4.写定时任务

package com.cn.springmvc.controller;

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author wyy
 * @date 2017年4月7日
 */
@Component
@Lazy(false)
@EnableScheduling  
public class TaskJob {  
    
    @Scheduled(cron = "0/5 * * * * *")  
    public void job1() {
        System.out.println("任务进行中。。。");  
    }
}

总结:

1.配置定时任务线程池可以同时执行同一时间的任务,否则是按照顺序执行。

2.如果xml里面开启的懒加载,default-lazy-init="true",需要有@Lazy(false)注解

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Mybatis 单独项目 下一篇装饰模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目