Spring注解@Scheduled定时任务
2024-08-22 04:10:15
一、首先配置applicationContext-task.xml
(1)添加 xmlns:task="http://www.springframework.org/schema/task"
(2)添加 xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
------- applicationContext-task.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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<description>spring task定时任务</description>
<!-- 定时任务配置 scheduler 方式 注解 -->
<context:component-scan base-package="com.lwj.task" />
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor"
scheduler="scheduler" />
</beans>
二、配置applicationContext.xml文件
------- applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 开启注解扫描 -->
<context:annotation-config /> <!--引入配置属性文件 -->
<context:property-placeholder location="classpath:config.properties" /> <!--自动扫描含有@Service将其注入为bean 和@Repository注入 -->
<context:component-scan base-package="com.lwj.service,com.lwj.dao" />
<!-- 定时任务 -->
<import resource="applicationContext-task.xml" />
</beans>
三、编写TimeTask.java
------- TimeTask.java文件 --------
package com.lwj.task; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
*
* @Description : 定时任务
* @author : lwj
* @version : 1.0
* @Date : 2016年4月7日 上午8:56:00
*/
@Component
public class TimeTask {
@Scheduled(cron="0/10 * * * * *")
public void Test(){
System.out.println("每10秒执行一次任务!");
}
}
效果图:
最新文章
- AEAI CRM_v1.5.2升级说明,开源客户关系管理系统
- log4php的配置
- AC日记——鬼谷子的钱袋 codevs 2998
- ubuntu 14.04 对exfat的支持
- HUDSON(Java开发的一种持续集成工具)
- 20145208 《Java程序设计》第6周学习总结
- 理解和熟练运用js中的call及apply
- AQL Subset Compiler:手把手教你如何写一个完整的编译器
- freemarker定义自己的标签错误(八)
- localStorage与sessionStorage区别
- Python菜鸟快乐游戏编程_pygame(1)
- redis的几种数据导入导出方式【转】
- CentOS7 Zabbix3.4安装
- 微信为什么不能下载apk 微信不能打开App下载地址的问题
- FineCMS v5.4.1 后台getshell
- sql语句查询排序
- 1043 Is It a Binary Search Tree (25 分)
- skopt学习之路1-函数介绍:dummy_minimize
- BZOJ 2243 染色 | 树链剖分模板题进阶版
- mavern安装方法