`
zhenther
  • 浏览: 23820 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

spring scheduled注解定时任务

阅读更多

spring mvc 使用注解执行定时任务

1.application配置文件加上

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.1.xsd"

<!-- 默认的定时任务注解映射支持 -->
    <task:annotation-driven />

2.

package com.wboss.web.scheduled;

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

/**
 * 邮件发送定时器
 * @author Wang Zhen
 *
 */
@Component
public class SingleMailScheduled {
   
    @Scheduled(cron="0/5 * * * * ?")   //每5秒执行一次
    public void exec() {
        System.out.println("发送邮件成功!");
    }
   
}

注:spring 在使用scheduled注解定时任务时,当application.xml中配置 default-lazy-init="true"时将不会执行定时任务,必须设置为false或去掉default-lazy-init,当然默认为false.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics