如何在spring中配置定时任务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在spring中配置定时任务相关的知识,希望对你有一定的参考价值。
在spring 中 基于注解的 定时配置很简单,只需要三步哦,如下:1、在类名前加@Component注解,标记该bean,也就是配置扫描标记。
2、在该类下的方法前加定是配置注解,@Schedule("cron= 0/30 * * * * *")。
3、添加配置文件(如下)。
实例如下:
1、class源文件。
package com.iss.ole.cggl.quartz;
import org.springframework.scheduling.annotation.Scheduled;
/**
* @function 订单计算 定时任务
* 1、试制订单定时计算
* a、车型拆分成零件需求
* b、需求生成订单明细
* 2、试装订单定时计算
* 3、工料废订单定时计算
* @author zhoujian
* @date 2014/10/29
*/
@Component
public class Quartz extends Base<a href="https://www.baidu.com/s?wd=Biz&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mhNhPj0kPhcYmhuhnH9W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPW6kPWbkn1nknj0dPWDzPWbz" target="_blank" class="baidu-highlight">Biz</a>
/** 车型拆分 成零件 <a href="https://www.baidu.com/s?wd=BIZ&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mhNhPj0kPhcYmhuhnH9W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPW6kPWbkn1nknj0dPWDzPWbz" target="_blank" class="baidu-highlight">BIZ</a>*/
private PlanManager<a href="https://www.baidu.com/s?wd=Biz&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mhNhPj0kPhcYmhuhnH9W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPW6kPWbkn1nknj0dPWDzPWbz" target="_blank" class="baidu-highlight">Biz</a> planManagerBiz;
/** 车型拆分为毛需求 */
@Scheduled(cron="0 0 06 * * ? ")
public void convertVehicleToParts()
try
planManagerBiz.createPartsList();
catch (BaseException e)
logger.error("");
e.printStackTrace();
public PlanManagerBiz getPlanManagerBiz()
return planManagerBiz;
public void setPlanManagerBiz(PlanManagerBiz planManagerBiz)
this.planManagerBiz = planManagerBiz;
2、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="<a href="http://www.springframework.org/schema/beans" " target="_blank">http://www.springframework.org/schema/beans" </a>
xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" " target="_blank">http://www.w3.org/2001/XMLSchema-instance" </a>
xmlns:context="<a href="http://www.springframework.org/schema/context"" target="_blank">http://www.springframework.org/schema/context"</a>
xmlns:p="<a href="http://www.springframework.org/schema/p" " target="_blank">http://www.springframework.org/schema/p" </a>
xmlns:task="<a href="http://www.springframework.org/schema/task"" target="_blank">http://www.springframework.org/schema/task"</a>
xmlns:tx="<a href="http://www.springframework.org/schema/tx"" target="_blank">http://www.springframework.org/schema/tx"</a>
xmlns:aop="<a href="http://www.springframework.org/schema/aop" " target="_blank">http://www.springframework.org/schema/aop" </a>
xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans " target="_blank">http://www.springframework.org/schema/beans </a>
<a href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" target="_blank">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a>
<a href="http://www.springframework.org/schema/context " target="_blank">http://www.springframework.org/schema/context </a>
<a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd" target="_blank">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>
<a href="http://www.springframework.org/schema/tx " target="_blank">http://www.springframework.org/schema/tx </a>
<a href="http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" target="_blank">http://www.springframework.org/schema/tx/spring-tx-3.0.xsd</a>
<a href="http://www.springframework.org/schema/task" target="_blank">http://www.springframework.org/schema/task</a>
<a href="http://www.springframework.org/schema/task/spring-task-3.0.xsd " target="_blank">http://www.springframework.org/schema/task/spring-task-3.0.xsd </a>
<a href="http://www.springframework.org/schema/aop " target="_blank">http://www.springframework.org/schema/aop </a>
<a href="http://www.springframework.org/schema/aop/spring-aop-3.0.xsd " target="_blank">http://www.springframework.org/schema/aop/spring-aop-3.0.xsd </a>
">
<!-- 扫描有相关标记的bean,初始化,交给spring管理-->
<context:component-scan base-package="com.iss.ole.cggl.quartz" />
<!-- 注入属性-->
<bean id="quartz" class="com.iss.ole.cggl.quartz.Quartz">
<property name="planManagerBiz">
<ref bean="planManagerBiz"/>
</property>
</bean>
</beans> 参考技术A 在spring 中 基于注解的 定时配置很简单,只需要三步哦,如下:
1、在类名前加@Component注解,标记该bean,也就是配置扫描标记。
2、在该类下的方法前加定是配置注解,@Schedule("cron= 0/30 * * * * *")。
3、添加配置文件(如下)。本回答被提问者采纳
以上是关于如何在spring中配置定时任务的主要内容,如果未能解决你的问题,请参考以下文章