Spring注解之 @EnableScheduling计划任务注解
要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,
然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务
示例:计划任务执行类
在这个类中的方法上需要@Scheduled注解配合@EnableScheduling使用。
package cn.hncu.p3.p3_taskscheduler; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created with IntelliJ IDEA.
* User:
* Date: 2016/11/22.
* Time: 下午 10:25.
* Explain:计划任务执行类
*/
@Service
public class ScheduledTaskService {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
public void reportCurrentTime(){
System.out.println("每隔5秒执行一次 "+dateFormat.format(new Date()));
} @Scheduled(cron = "0 07 20 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天20点07分执行;
//cron是UNIX和类UNIX(Linux)系统下的定时任务
public void fixTimeExecution(){
System.out.println("在指定时间 "+dateFormat.format(new Date())+" 执行");
}
}
配置类
通过@EnableScheduling注解开启对计划任务的支持
package cn.hncu.p3.p3_taskscheduler; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* Created with IntelliJ IDEA.
* User:
* Date: 2016/11/22.
* Time: 下午 10:32.
* Explain:配置类
*/ @Configuration
@ComponentScan("cn.hncu.p3.p3_taskscheduler")
@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
public class TaskScheduleConfig {
}
运行结果
package cn.hncu.p3.p3_taskscheduler; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Created with IntelliJ IDEA.
* User:
* Date: 2016/11/22.
* Time: 下午 10:34.
* Explain:运行类
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskScheduleConfig.class);
}
}
运行结果
Spring注解之 @EnableScheduling计划任务注解的更多相关文章
- Spring boot @EnableScheduling 和 @Scheduled 注解使用例子
前言 Spring Boot提供了@EnableScheduling和@Scheduled注解,用于支持定时任务的执行,那么接下来就让我们学习下如何使用吧: 假设我们需要每隔10秒执行一个任务,那么我 ...
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件
本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...
- Spring 02多种注入方式和注解实现DI
一.Bean作用域 spring容器创建的时候,会将所有配置的bean对象创建出来,默认bean都是单例的.代码通过getBean()方法从容器获取指定的bean实例,容器首先会调用Bean类的无参构 ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- spring框架学习(五)注解
注解Annotation,是一种类似注释的机制,在代码中添加注解可以在之后某时间使用这些信息.跟注释不同的是,注释是给我们看的,Java虚拟机不会编译,注解也是不编译的,但是我们可以通过反射机制去读取 ...
- spring注解说明之Spring2.5 注解介绍(3.0通用)
spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...
- spring + myBatis 常见错误:@Autowired注解失败
今天配置spring+myBatis的时候,使用注解@Autowired把持久层dao注入service层的时候总是报错. 查了好久才发现,居然是配置文件路径写错了.basepackge的路径一定要正 ...
- Spring入门(6)-使用注解装配
Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...
- 从 Spring 2.5 开始就可以使用注解来配置依赖注入,而不是采用 XML 来描述一个 bean。
1.在 XML 注入之前进行注解注入,因此后者可以被前者重写. 2.在默认情况下注解在 Spring 容器中不打开,需要配置启动. <beans xmlns="http://www.s ...
随机推荐
- sublime 代码段
demo 展示助手中有经常用到个标签. <textarea type="text/md_x" style="display:none"> ## de ...
- Javascript 日期格式化 相关操作
1.相关扩展函数 //--------------------------------------------------- // 判断闰年 //--------------------------- ...
- JavaScript——执行环境、变量对象、作用域链
前言 这几天在看<javascript高级程序设计>,看到执行环境和作用域链的时候,就有些模糊了.书中还是讲的不够具体.通过上网查资料,特来总结,以备回顾和修正. 目录: EC(执行环境或 ...
- window7安装python的xgboost库方法
window7安装python的xgboost库方法 1.下载xgboost-master.zip文件,而不是xgboost-0.4a30.tar.gz,xgboost-0.4a30.tar.gz是更 ...
- 【Python】【数据库】
#[[数据库]]'''MySQL是Web世界中使用最广泛的数据库服务器.SQLite的特点是轻量级.可嵌入,但不能承受高并发访问,适合桌面和移动应用.而MySQL是为服务器端设计的数据库,能承受高并发 ...
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- curl: (6) Could not resolve host: www.baidu.com;
今天,在执行curl时,突然发现这个报错,问题是之前完全没有出现过这样的情况. [root@localhost ~]# curl www.baidu.comcurl: (6) Could not re ...
- Android之StrictMode
1. StrictMode是什么? StrictMode is a developer tool which detects things you might be doing by accident ...
- GreenDao3使用完全解析
1,gradle配置(官网样例地址https://github.com/greenrobot/greenDAO/blob/master/examples/RxDaoExample/build.grad ...
- 用友软件系统管理员账号admin密码忘记了,如何解决?
1.打开数据库. 2.点开 数据库-UFSystem. 3.找到dbo.UA_user表,鼠标右键,点打开表. 4.打开后,找到admin,cPassword列即可找到系统管理员密码.