Spring task基本使用

来源:互联网 发布:oppo手机的mac地址 编辑:程序博客网 时间:2024/06/02 05:28

不需要添加其他的jar,只要Spring相关的包即可:主要是spring-context包

<dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.3.8.RELEASE</version>        </dependency>    </dependencies>

Spring.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:p="http://www.springframework.org/schema/p"     xmlns:context="http://www.springframework.org/schema/context"       xmlns:cache="http://www.springframework.org/schema/cache"       xmlns:task="http://www.springframework.org/schema/task"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.3.xsd        http://www.springframework.org/schema/cache        http://www.springframework.org/schema/cache/spring-cache-4.3.xsd        http://www.springframework.org/schema/task        http://www.springframework.org/schema/task/spring-task-4.3.xsd"><context:component-scan base-package="net.imwork.redis.service.impl" />    <context:annotation-config /><task:annotation-driven/></beans>

定时任务类(要能够被扫描到):

package net.imwork.redis.service.impl;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Component("task")public class Taskutil {    //{秒} {分} {时} {日期(具体哪天)} {月} {星期}    @Scheduled(cron = "30 * * * * ?")//// 表达的含义是:每半分钟触发一次任务    public void job(){        System.out.println("正在执行任务。。。");    }}

应用启动之后执行结果:
这里写图片描述

原创粉丝点击