一、定义配置类

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. selenium+python3 鼠标事件

    1.鼠标右击 ActionChains(driver).contest_click(right_click).perform() 2.鼠标悬停 ActionChains(driver).move_to ...

  2. U14739 X ask Y III 子区间异或和

    题意:就是求所有子区间的异或和的和 题解:就是算每一位对结果的贡献(最近好像遇到很多次这种题目),先前缀异或,从左向右扫记录二进制前缀的1,0个数,xor[i]==xor[j]^1的时候就加上这一位的 ...

  3. svn上传文件钩子

    svn钩子 钩子脚本就是shell的写法,钩子就是被某些版本库事件触发的程序. 常用钩子: post-commit:在提交完成成功创建之后执行该钩子.(提交已经完成,不可更改) 更新之后,通过邮件.微 ...

  4. 分布式_理论_02_Base 理论

    一.前言 五.参考资料 1.分布式理论(二)——Base 理论 2.分布式理论(二) - BASE理论

  5. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  6. New Concept English three (58)

    30w/m 76 errors The old lady was glad to be back at the block of flats where she lived. Her shopping ...

  7. skynet coroutine 运行笔记

    阅读云大的博客以及网上关于 skynet 的文章,总是会谈服务与消息.不怎么看得懂代码,光读这些文字真的很空洞,不明白说啥.网络的力量是伟大的,相信总能找到一些解决自己疑惑的文章.然后找到了这篇讲解 ...

  8. CodeForces - 794C:Naming Company(博弈&简单贪心)

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...

  9. Brackets (区间DP)

    个人心得:今天就做了这些区间DP,这一题开始想用最长子序列那些套路的,后面发现不满足无后效性的问题,即(,)的配对 对结果有一定的影响,后面想着就用上一题的思想就慢慢的从小一步一步递增,后面想着越来越 ...

  10. bzoj 3629 [JLOI2014]聪明的燕姿——约数和定理+dfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3629 如果要搜索,肯定得质因数分解吧:就应该朝这个方向想. **约数和定理: 对于任意一个大 ...