spring定时任务

spring定时任务有两种实现方式:

1、注解方式

2、XML配置

在具体实现之前,我们需要在spring配置中引入一些东西:

xmlns 中的 xmlns:task="http://www.springframework.org/schema/task"

xsi 中的 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

引入job包:<context:component-scan base-package="job" /> 

具体配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">


<!-- 配置定时任务 -->
<!-- <task:scheduled-tasks>
<task:scheduled ref="TestScheduledJob" method="handleTask" cron="0/5 * * * * ?"/>
</task:scheduled-tasks> -->


<context:component-scan base-package="job" />

<task:annotation-driven/>
</beans>

1、注解方式(每五秒执行一次)

2、XML配置方式

原文地址:https://www.cnblogs.com/chun-chun/p/9563097.html