Spring注解-TaskScheduler
一、定义配置类
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的更多相关文章
- spring注解源码分析--how does autowired works?
1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...
- Spring注解
AccountController .java Java代码 1. /** 2. * 2010-1-23 3. */ 4. packag ...
- spring 注解的优点缺点
注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...
- spring注解说明之Spring2.5 注解介绍(3.0通用)
spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...
- 使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...
- spring注解scheduled实现定时任务
只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...
- [转]Spring 注解总结
原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...
- eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式
eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...
- Spring注解【非单例】
花了至少一整天的时间解决了这个问题,必须记录这个纠结的过程,问题不可怕,思路很绕弯. 为了能说清楚自己的问题,我都用例子来模拟. 我有一个类MyThread是这样的: @Service public ...
随机推荐
- html5 pc端参考网址
http://huodong.baidu.com/zhuanpan/?SEM&PC&refer=107255
- crontab 总结
crontab -e --------- linux定时任务提示-bash: crontab: command not found 执行 crontab 命令如果报 command not found ...
- vue中的懒加载和按需加载
懒加载 (1)定义:懒加载也叫延迟加载,即在需要的时候进行加载,随用随载. (2)异步加载的三种表示方法: 1. resolve => require([URL], resolve),支持性好 ...
- 02-THREE.JS 辅助线使用
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- js中判断数据类型
一般来说,可以使用typeof来判断数据类型,但是数组,对象和null的结果都是object,那么如何区分这三类呢?可以使用如下方法: var arr = []; var obj = {} var e ...
- hdoj-1027-Ignatius and the Princess II(逆康拓展开)
题目链接 /* Name: Copyright: Author: Date: 2018/5/2 11:07:16 Description:输出第m小的序列 */ #include <iostre ...
- hdoj-1715-大菲波数(大斐波那契数列)
题目链接 import java.util.*; import java.math.*; public class Main{ public static void main(String[] arg ...
- 学习汤姆大叔《深入理解JavaScript系列》有感(1) —— 立即调用的函数表达式
一. 下面代码用于理解函数的声明和调用. function makeCounter() { // 只能在makeCounter内部访问i var i = 0; return function () { ...
- 20179203 《Linux内核原理与分析》第十二周作业
Return-to-libc 攻击实验 一.实验描述 缓冲区溢出的常用攻击方法是用 shellcode 的地址来覆盖漏洞程序的返回地址,使得漏洞程序去执行存放在栈中 shellcode.为了阻止这种类 ...
- HAWQ 官方文档创建filespace,tablespace,database,table
1.创建Filespace 创建Filespace必须是数据库超级用户( You must be a database superuser to create a filespace.)首先创建一个f ...