spring3.0定时任务

<spring3.0定时任务>

 

在项目中经常用到的spring的一个功能就是定时任务,它可以自动监视时间,到点就执行,给程序带来了很大的方便,很多地方都会需要这种功能,比如做数据备份、同步等操作。最近一直比较忙,主要是比较懒,今天把这部分稍作小结。

       使用spring定时任务的前提:项目中已经搭建好了spring环境(我用的是spring3.0)。

       一、基本使用:

        spring的定时任务使用起来十分方便,只需要两步:1、写好执行定时任务的类和方法;2、配置spring定时任务配置文件:

1.  package com.test;  

2.    

3.  public class Test {  

4.    

5.      public void test() {  

6.          System.out.println("执行定时任务的方法。");  

7.      }  

8.  }  

2、配置spring的定时任务配置文件(可以新建一个xml文件,也可以在已经有的xml文件中配置)

配置文件内容:

1.  <?xml version="1.0" encoding="UTF-8"?>  

2.  <beans xmlns="http://www.springframework.org/schema/beans"  

3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

4.         xmlns:aop="http://www.springframework.org/schema/aop"  

5.         xmlns:tx="http://www.springframework.org/schema/tx"  

6.         xmlns:context="http://www.springframework.org/schema/context"  

7.         xsi:schemaLocation="  

8.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  

10.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  

11.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

12.  

13.   <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  

14.         

15.        <!--必须,QuartzScheduler 延时启动,应用启动后 QuartzScheduler 再启动-->  

16.        <property name="startupDelay" value="60"/>          

17.        <!-- 普通触发器 :触发器列表-->  

18.        <property name="triggers">  

19.            <list>                  

20.                <ref local="<SPAN style="COLOR: #ff0000">testTrigger</SPAN>"/>  

21.            </list>  

22.        </property>  

23.    </bean>  

24.  

25.<!-- 配置执行定时任务的类和方法 -->   

26.    <bean id="<SPAN style="COLOR: #ff0000">testDetail</SPAN>"    

27.        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    

28.        <property name="targetObject">    

29.            <bean class="<SPAN style="COLOR: #ff0000">com.test.Test</SPAN>"></bean>  

30.        </property>    

31.        <property name="targetMethod">    

32.            <value><SPAN style="COLOR: #ff0000">test</SPAN></value>    

33.        </property>    

34.    </bean>  

 

 

 

1.  <!-- 配置触发器 -->     

2.      <bean id="<SPAN style="COLOR: #ff0000">testTrigger</SPAN>"    

3.          class="org.springframework.scheduling.quartz.CronTriggerBean">    

4.          <property name="jobDetail">    

5.              <ref bean="<SPAN style="COLOR: #ff0000">testDetail</SPAN>"/> <!-- 触发器触发的 执行定时任务的bean -->     

6.          </property>    

7.          <property name="cronExpression">    

8.              <!-- 每天23   -->  <!-- 定时任务执行的间隔 -->   

9.              <value>0 0 23 * * ?</value>  

10.        </property>    

11.    </bean>    

12.</beans>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Scheduled定时任务是一种在Spring Boot中创建定时任务的方式。目前主要有三种创建方式: 1. 基于注解(@Scheduled)的静态任务:通过在方法上添加@Scheduled注解来指定任务的执行时间。 2. 基于接口(SchedulingConfigurer)的动态任务:通过实现SchedulingConfigurer接口,可以根据数据库的内容动态调度任务。 3. 基于注解的多线程定时任务:通过使用@Scheduled注解和多线程来实现定时任务的并发执行。 在使用Spring Scheduled定时任务时,需要在启动类上添加@EnableScheduling注解来开启定时任务功能。然后可以在方法上使用@Scheduled注解来指定任务的执行时间,或者实现SchedulingConfigurer接口来添加定时任务。同时,可以配置定时任务的多线程非阻塞运行,以提高任务的并发性能。 以上是关于Spring Scheduled定时任务的简要介绍和使用方式。如果需要更详细的信息,可以参考引用\[1\]和引用\[2\]中的内容。 #### 引用[.reference_title] - *1* [SpringBoot之Scheduled定时任务详解](https://blog.csdn.net/weixin_41003771/article/details/102655202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [spring schedule定时任务详解](https://blog.csdn.net/qq_34480904/article/details/122410711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值