一、定义配置类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* 定时器的配置类
* @author DUCHONG
* @since 2017-08-15 9:51
**/
@Configuration
@ComponentScan("com.duchong.springboot.demo")
@EnableScheduling
public class SchedulerConfig {
}

二、执行方法

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import java.text.SimpleDateFormat;
import java.util.Date; /**
* 定时器的服务
* @author DUCHONG
* @since 2017-08-15 9:55
**/
@Service
public class ScheduledService {
private static final SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000)
public void schedule1(){
System.out.println("fixedRate--每五秒执行一次:"+sdf.format(new Date()));
} @Scheduled(cron = "0/1 * * * * ?")
public void schedule2(){
System.out.println("cron表达式--每一秒执行一次:"+sdf.format(new Date()));
}
}

三、Main方法

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* 定时任务的启动类
* @author DUCHONG
* @since 2017-08-15 10:13
**/
public class SchedulerMain { public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(SchedulerConfig.class);
} }

四、结果

顺便说一句,spring全注解真的很好用。

Spring注解-TaskScheduler的更多相关文章

  1. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  2. Spring注解

    AccountController .java Java代码   1.        /** 2.         * 2010-1-23 3.         */ 4.        packag ...

  3. spring 注解的优点缺点

    注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...

  4. spring注解说明之Spring2.5 注解介绍(3.0通用)

    spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...

  5. 使用Spring注解来简化ssh框架的代码编写

     目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...

  6. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  7. [转]Spring 注解总结

    原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...

  8. eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式

    eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...

  9. Spring注解【非单例】

    花了至少一整天的时间解决了这个问题,必须记录这个纠结的过程,问题不可怕,思路很绕弯. 为了能说清楚自己的问题,我都用例子来模拟. 我有一个类MyThread是这样的: @Service public ...

随机推荐

  1. Java中使用注释

    在编写程序时,经常需要添加一些注释,用以描述某段代码的作用. 一般来说,对于一份规范的程序源代码而言,注释应该占到源代码的 1/3 以上.因此,注释是程序源代码的重要组成部分,一定要加以重视哦! Ja ...

  2. JMeter正则表达式提取器说明

    Apply to:应用范围 要检查的响应字段:样本数据源. 引用名称:其他地方引用时的变量名称,引用方法:${引用名称} 正则表达式:数据提取器,如上图的 "sysNo":&quo ...

  3. postgresql查看数据库占用的物理存储空间大小

    1.手动查看: 查看数据库postgres的oid postgres=# SELECT oid from pg_database where datname='postgres'; oid------ ...

  4. 深入剖析Redis主从复制

    [http://sofar.blog.51cto.com/353572/1413024/]   [Redis 主从复制的内部协议和机制]   一.主从概述 Redis 支持 Master-Slave( ...

  5. Github 配置 SSH

    HTTPS方式管理的直接输入登录用户名和密码就可以.下面介绍SSH的方式. mac系统为例,官方ssh参考:https://help.github.com/articles/generating-ss ...

  6. Codeforces Round #259(div2)C(数学期望)

    数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k ...

  7. CCTextFieldTTF 与 5种常用CCMenuItem

    //继承(class HelloWorld : public cocos2d::CCLayer, public cocos2d::CCTextFieldDelegate) CCTextFieldTTF ...

  8. UVA - 1331 Minimax Triangulation (区间dp)(最优三角剖分)

    题目链接 把一个多边形剖分成若干个三角形,使得其中最大的三角形面积最小. 比较经典的一道dp问题 设dp[l][r]为把多边形[l,r]剖分成三角形的最大三角形面积中的最小值,则$dp[l][r]=m ...

  9. django的工作图

  10. C++对C语言的拓展(4)—— 函数重载

    函数重载(Function Overload):用同一个函数名定义不同的函数,当函数名和不同的参数搭配时函数的含义不同. 1.重载规则 (1)函数名相同: (2)参数个数不同,参数的类型不同,参数顺序 ...