Spring 使用注解配置cron

2 篇文章 0 订阅
1 篇文章 0 订阅

参考:spring自带的定时任务功能(task)

主要步骤

  • 在applicationContext.xml中添加
  xmlns:task="http://www.springframework.org/schema/task"  

  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  • 在applicationContext.xml中配置定时器开关
<!-- 定时器开关-->
<task:annotation-driven /> 
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.kyl.scheduledtask.statictask.annotation" />
  • 参考下面完整代码配置完后,直接运行main方法,可直接在控制台查看结果

注意事项

  • spring3.x可以通过标签轻易的定义定时任务,而且不需要依赖第三方jar包如quartz等,这个是Spring自己的实现,但不支持集群,其cron表达式也不支持年。
  • Spring4.0版本以上支持动态改变cron的值
  • 如果使用注解的话,改变cron的值需要重新编译java类,而使用xml设置cron则不需要

完整代码

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <task:annotation-driven /> <!-- 定时器开关-->
    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.kyl.scheduledtask.statictask.annotation" />
</beans>

MyTaskAnnotation.java

package com.kyl.scheduledtask.statictask.annotation;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 基于注解的定时器
 * @author kyl
 */
@Component
public class MyTaskAnnotation {

    /**
     * 每天5s执行一次
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void test1(){
        System.out.println("111111111");
    }
    /**
     * 每隔2秒执行一次
     */
    @Scheduled(fixedRate = 1000*2)
    public void test2(){
        System.out.println("222222222");
    }
}

Main.java

package com.kyl.scheduledtask.statictask.annotation;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        new ClassPathXmlApplicationContext("classpath:com//kyl//scheduledtask//statictask//annotation//applicationContext.xml");
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wisely</groupId>
    <artifactId>highlight_spring4</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>10</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值