一、首先配置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 UICollectionView之二(垂直滚动)

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  2. sqlplus登陆

    cd \sqlplus sys@test_id as sysdba 切换用户SQL> connect system@test_id

  3. jdk安装与环境变量配置(一劳永逸)

    换了一个项目组做新的项目,新的机器,又得重新打环境,懒得去百度下,这里做个备份,下回直接看就行,如下 进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1.下载jdk(http ...

  4. Leetcode: Convert a Number to Hexadecimal

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...

  5. JS语法部分

    定义变量使用通用类型var:字符串(需要引号),小数,整数,布尔型(只返回对或错),日期时间 算术运算符:+  —  *  /  %(1取余数,2判断是不是整数,3将某个数值变为某个范围之内的数,4判 ...

  6. HTML调用servlet(一)

    1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...

  7. html a标签链接使用action 参数传递中文乱码

    <a href="queryByType?ptype=鼠标"> 在后台变量ptype接收的值为乱码 解决方法: 在tomcat的server.xml文件中添加 URIE ...

  8. The Havel-Hakimi Algorithm

    1.If any di>=n then fail 2.If there is an odd number of odd degrees then fail 3.If there is a di& ...

  9. UVa10023手动开大数平方算法

    题目链接:UVa 10023 import java.math.BigInteger; import java.util.Scanner; public class Main { public sta ...

  10. extjs 4.2 日期控件 选择时分秒功能

    因为不支持时分秒,然后在网上也找了一段时间的插件,但是感觉起来都不大方便,最后找一个插件,只需要引用js文件,然后修改类型,就可以实现extjs下面的datafield带时分秒功能了. 步骤: 只需要 ...