一、首先配置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定时任务的更多相关文章

  1. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  2. spring注解@Scheduled中fixedDelay、fixedRate和cron表达式的区别

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Spring Boot Scheduled定时任务特性

    SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...

  4. spring的@Scheduled定时任务,同一时间段的定时任务只会执行一个,其余的会被阻塞,@Scheduled注解定时任务并发执行解决办法,即多线程运行定时任务

    原文:https://blog.csdn.net/qq_35937303/article/details/88851064 现有两个定时任务 @Component("aa") pu ...

  5. Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 任务调度动态改变时间

    不需要重启应用就可以动态的改变Cron表达式的值 import java.util.Date; import java.util.concurrent.Executor; import java.ut ...

  6. Spring注解配置定时任务<task:annotation-driven/>

    http://m.blog.csdn.net/article/details?id=50945311 首先在配置文件头部的必须要有: xmlns:task="http://www.sprin ...

  7. spring注解 @Scheduled(cron = "0 0 1 * * *")实现定时的执行任务

    @Scheduled(cron = "0 0 1 * * *") 在使用该注解以前请做好以下准备工作,配置好相应的xm文件. 配置定时注解的步骤:http://blog.csdn. ...

  8. Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 动态改变时间

    import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.Executors; ...

  9. Spring 的@Scheduled注解实现定时任务运行和调度

    Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xm ...

随机推荐

  1. iOS:特殊符号大全

    特殊符号大全分享给大家,直接复制粘贴就可以使用了! ░ ▒ ▬ ♦ ◊ ◦ ♠ ♣ ▣ ۰•● ❤ ●•۰ ► ◄ ▧ ▨ ♨ ◐ ◑ ↔ ↕ ▪ ▫ ☼ ♦ ♧♡♂♀♠♣♥❤☜☞☎☏⊙◎ ☺☻☼▧▨ ...

  2. zabbix监控activemq队列脚本

    公司业务使用activemq5.9.1消息队列,由于队列阻塞导致程序端口无响应,并且telnet无法连通.经过over 1 hour的排查,最终定位原因activemq导致.遂写了一个监控active ...

  3. 在Ubuntu 14.04 上安装网易云音乐

    之前因为电脑有网络的原因,一直使用网页网易云音乐听歌,最近电脑没网络使用,才发现网易云音乐有linux版本,果断下载. 在Chrome浏览器中,登陆官网下载Linux版本中的Ubuntu 14.04 ...

  4. 分享一下一款直播App开发的过程

    听说有人声称开发一款直播App不仅耗时还非常昂贵,今天跟大家说道一下,开发一款直播App到底分几步走? 第一步:分解直播App的功能,我们以X客为例 视频直播功能,这是一款直播App最主要的功能,要能 ...

  5. [MySQL] 变量(参数)的查看和设置

    类似于Oracle的参数文件,MySQL的选项文件(如my.cnf)用于配置MySQL服务器,但和Oracle叫法不一样,在MySQL里, 官方叫变量(Varialbes),但其实叫参数也是可以的,只 ...

  6. mysql centeros 安装

    http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html linux mysql允许远程连接 1.登录数据库:my ...

  7. Perl Print Win32 Console Windows 控制台 print Unicode 问题

    参考资料: https://stackoverflow.com/questions/15224400/perl-on-windows-problems-with-encoding https://te ...

  8. Java基础之访问文件与目录——测试文件或目录的路径(TryPath)

    控制台程序,测试文件或目录的路径. import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.FileSy ...

  9. 4.Spring Web MVC处理请求的流程

  10. LaTeX 有哪些「新手须知」的内容?

    孟晨 ,在 LaTeX 话题下写错 LaTeX 名字的,一律… 陈硕等 137 人赞同 这是个好问题,虽然提问提得很大.不是很好答,权当抛砖引玉了. 天字第一号原则:不要到网上抄代码,尤其是似懂非懂的 ...