Spring注解@Scheduled定时任务
一、首先配置applicationContext-task.xml
(1)添加 xmlns:task="http://www.springframework.org/schema/task"
(2)添加 xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
------- applicationContext-task.xml文件 --------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<description>spring task定时任务</description>
<!-- 定时任务配置 scheduler 方式 注解 -->
<context:component-scan base-package="com.lwj.task" />
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor"
scheduler="scheduler" />
</beans>
二、配置applicationContext.xml文件
------- applicationContext.xml文件 --------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 开启注解扫描 -->
<context:annotation-config /> <!--引入配置属性文件 -->
<context:property-placeholder location="classpath:config.properties" /> <!--自动扫描含有@Service将其注入为bean 和@Repository注入 -->
<context:component-scan base-package="com.lwj.service,com.lwj.dao" />
<!-- 定时任务 -->
<import resource="applicationContext-task.xml" />
</beans>
三、编写TimeTask.java
------- TimeTask.java文件 --------
package com.lwj.task; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
*
* @Description : 定时任务
* @author : lwj
* @version : 1.0
* @Date : 2016年4月7日 上午8:56:00
*/
@Component
public class TimeTask {
@Scheduled(cron="0/10 * * * * *")
public void Test(){
System.out.println("每10秒执行一次任务!");
}
}
效果图:

Spring注解@Scheduled定时任务的更多相关文章
- spring注解scheduled实现定时任务
只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...
- spring注解@Scheduled中fixedDelay、fixedRate和cron表达式的区别
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring Boot Scheduled定时任务特性
SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...
- spring的@Scheduled定时任务,同一时间段的定时任务只会执行一个,其余的会被阻塞,@Scheduled注解定时任务并发执行解决办法,即多线程运行定时任务
原文:https://blog.csdn.net/qq_35937303/article/details/88851064 现有两个定时任务 @Component("aa") pu ...
- Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 任务调度动态改变时间
不需要重启应用就可以动态的改变Cron表达式的值 import java.util.Date; import java.util.concurrent.Executor; import java.ut ...
- Spring注解配置定时任务<task:annotation-driven/>
http://m.blog.csdn.net/article/details?id=50945311 首先在配置文件头部的必须要有: xmlns:task="http://www.sprin ...
- spring注解 @Scheduled(cron = "0 0 1 * * *")实现定时的执行任务
@Scheduled(cron = "0 0 1 * * *") 在使用该注解以前请做好以下准备工作,配置好相应的xm文件. 配置定时注解的步骤:http://blog.csdn. ...
- Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 动态改变时间
import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.Executors; ...
- Spring 的@Scheduled注解实现定时任务运行和调度
Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml --- 即spring的主配置文件(有的项目中叫做applicationContext.xm ...
随机推荐
- navicat 链接不上mysql
1 查看 my.cnf 的配置bindhost 127.0.0.1 注释掉 2 grant all privileges on *.* to root@'%' identified by 'passw ...
- Android图片压缩方法总结
本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 ...
- 安装Cocoapods(Pods 管理iOS 第三方库)
安装 可先检测Mac电脑是否安装Pods.打开控制台: $ which pod 如果安装,结果如下;如果没有安装,控制台无反应. /usr/bin/pod 如果没有安装, 以下命令安装之. $ sud ...
- 查看python的路径
>>> import sys >>> sys.path
- Pytho实现tail -f
实现Python版的tail -f功能 tail -f 的功能非常好用.我们用Python也可以实现这样的功能.实现的原理是通过Python版本的inotify获得文件的更新消息,从而读取更新的行.p ...
- Java socket中关闭IO流后,发生什么事?(以关闭输出流为例)
声明:该博文以socket中,关闭输出流为例进行说明. 为了方便讲解,我们把DataOutputstream dout = new DataOutputStream(new BufferedOutpu ...
- Leetcode: Insert Delete GetRandom O(1)
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- Lintcode: Minimum Adjustment Cost
Given an integer array, adjust each integers so that the difference of every adjcent integers are no ...
- 一个新人对JavaScript的内容简单介绍
JavaScript 1.基本的数据类型:字符串 小数 整数 时间日期 布尔型等. 2.变量: JS定义变量通通都是用var开头,var里面可以放任何东西(如:小数,整数,字符串,时间日期等等 ...
- Nginx简介
序言Nginx 是 lgor Sysoev 为俄罗斯访问量第二的 rambler.ru 站点设计开发的.从 2004 年发布至今,凭借开源的力量,已经接近成熟与完善.Nginx 功能丰富,可作为 HT ...