SpringMvc自动任务调度之task实现项目源码,@Scheduled

1.Xml配置 Spring-job.xml 并在 Spring-Application.xml中Import

复制代码
<?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:aop="http://www.springframework.org/schema/aop"
       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.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

    <description>Spring Configuration</description>
    <context:component-scan base-package="包的根目录例:com.xx.xx"/>
    <!-- 配置任务线性池 -->
    <task:executor  id="executor" pool-size="10" />
    <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
    <!-- 如果类文件中没有使用@Scheduled ,可以使用下面的配置 -->
    <!--<task:scheduled-tasks scheduler="scheduler">
        <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>
    </task:scheduled-tasks>-->
</beans>
复制代码

 

2.类文件TASK.java

复制代码
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("TestJob")
public class ATask{
    @Scheduled(cron = "0 9 16 * * ?")//每隔5秒隔行一次
    public void test(){
        System.out.println("job1 开始执行。。。。");
    }
}
复制代码

 

然后就可以了;

原文地址:https://blog.csdn.net/zhulin2012/article/details/51916612

posted @   Bodi  阅读(527)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示