一、定义配置类

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. vs plug

    工欲善其事,必先利其器.尽管visual studio本身已经非常强大,但优秀的插件仍然可以帮开发者大大提高效率,以下是牛牛非常喜欢的vs插件. 1.Indent Guides 绝对是必须的,有了这些 ...

  2. web自动化:元素定位(二)

    一. 实例 如何定位到下图第二个"抢投标",有一种方法是利用xpath定位 //a[@href="/loan/loan_detail/Id/7190.html" ...

  3. IDEA 上传更新的代码到码云上

    1.Commit Changes 2. .

  4. 分布式_事务_01_2PC框架raincat快速体验1

    一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...

  5. nyoj-1132-promise me a medal(求线段交点)

    题目链接 /* Name:nyoj-1132-promise me a medal Copyright: Author: Date: 2018/4/26 20:26:22 Description: 向 ...

  6. WC2019 I 君的商店

    交互题 一个 01 序列,告诉你其中 1 有奇数个还是偶数个,每次可以给定两个集合 $A$,$B$,系统会告诉你 $A \leq B$ 或者 $B \leq A$ 求序列 交互次数要求 $5n + O ...

  7. asp.net core mcroservices 架构之 分布式日志(二)之自定义日志开发

    netcore日志原理 netcore的日志是作为一个扩展库存在的,每个组件都有它的入口,那么作为研究这个组件的入口是最好的,首先看两种方式: 这个是源码例子提供的. var loggingConfi ...

  8. 每天一个linux命令(14):less命令

    版权声明更新:2017-05-18博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...

  9. 前端项目使用module.exports文件一定要Webpack编译吗?请问gulp可以编译这种文件吗

    import引入类似这种文件,一定要用webpack去编译吗 module.pxports 是CMD规范的一个全局函数,功能是当前模块对外提供接口.require可以直接使用这个接口.例子: echo ...

  10. webpack学习(一)—— 入门

    ,我们通常采用的是组件化开发方式,这样就会对应有很多个js文件,而打包工具的出现则是为了正确处理这些js文件的依赖关系,并生成一个最终的文件,这样,我们最后只需要加载打包以后的文件就可以了,而无须加载 ...