spirng定时任务的两种配置:注解和xml
一 使用注解Task
1、在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"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd ">
<!-- 扫描路径-->
<context:component-scan base-package="com.mediaforce.news.api.quartz" />
<task:executor id="executor" pool-size="1" />
<task:scheduler id="scheduler" pool-size="1" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
2、在任务类中使用注解
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component(“taskJob”)
public class TaskJob {
@Scheduled(cron = "0 0 3 * * ?")
public void job1() {
System.out.println(“任务进行中。。。”);
}
}
二、使用xml配置quarz
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!-- 微信抓取 -->
<bean id="weixinJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="taskJob" /> //目标类
</property>
<property name="targetMethod">
<value>weixin</value> //目标执行方法
</property>
</bean>
<!-- 微信抓取,1分钟一次 -->
<bean id="cronWeixinJobDetail" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="weixinJobDetail" />
</property>
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>
<!-- 执行定时任务-->
<bean id="zz" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronWeixinJobDetail" />
</list>
</property>
</bean>
</beans>
spirng定时任务的两种配置:注解和xml的更多相关文章
- Hibernate实现有两种配置,xml配置与注释配置
hibernate实现有两种配置,xml配置与注释配置. (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) < ...
- hibernate实现有两种配置,xml配置与注释配置。<转>
<注意:在配置时hibernate的下载的版本一定确保正确,因为不同版本导入的jar包可能不一样,所以会导致出现一些错误> hibernate实现有两种配置,xml配置与注释配置. (1) ...
- cron 定时任两种配置方式
第一种:xml文件方式 <bean id="commonTimer" class="com.course.wx.timer.CommonTimer"> ...
- 使用java配置定时任务的几种配置方式及示例
Spring定时器,主要有两种实现方式,包括Java Timer定时和Quartz定时器! 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 package ...
- Linux下实现秒级定时任务的两种方案
Linux下实现秒级定时任务的两种方案(Crontab 每秒运行): 第一种方案,当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间. while true ;do command s ...
- hibernate 一对一 one to one的两种配置方式
hibernate中one-to-one两种配置方式 标签: hibernateHibernateone-to-one 2013-02-19 17:44 11445人阅读 评论(1) 收藏 举报 分 ...
- 浅谈Spring的两种配置容器
浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41 作者:黄小鱼ZZZ ...
- Hibernate中双向多对多的两种配置方式
Hibernate中双向多对多的两种配置方式 1.建立多对多双向关联关系 package cn.happy.entitys; import java.util.HashSet; import java ...
- flask框架--设置配置文件的几种方式 与Flask两种配置路由的方式
设置配置文件的几种方式 ==========方式一:============ app.config['SESSION_COOKIE_NAME'] = 'session_lvning' #这种方式要把所 ...
随机推荐
- POJ 2195:Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...
- python: HTML中的选择器
id选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- spring netty-socket.io
spring-nettysocketio.xml文件内容如下: <?xml version="1.0" encoding="UTF-8"?>< ...
- minicom 使用教程
因为现在电脑基本不配备串行接口,所以,usb转串口成为硬件调试时的必然选择.目前知道的,PL2303的驱动是有的,在dev下的名称是ttyUSB#. minicom,tkterm都是linux下应用比 ...
- 《BI那点儿事》三国数据分析系列——蜀汉五虎上将与魏五子良将武力分析,绝对的经典分析
献给广大的三国爱好者们,希望喜欢三国的朋友一起讨论,加深对传奇三国时代的了解 数据分析基础概念:集中趋势分析是指在大量测评数据分布中,测评数据向某点集中的情况.总体(population)是指客观存在 ...
- memcache内存估算整理
参考文章: http://blog.csdn.net/tonyxf121/article/details/7906428 http://zhihuzeye.com/archives/2361 memc ...
- [Selenium] 拖拽一个 Component 到 Workspace
先使Component可见,获取Component位置信息,获取Workspace位置信息,点击Component并拖拽到Workspace,最后释放.(调试时dragAndDropOffset()方 ...
- TextView 行数控制
控制textview只显示 2 行文本,超过 2 行显示展开/折叠按钮(mShowImage),isShow=true时,是展开状态,isShow=false时,是折叠状态 方法一: int line ...
- MDK的一些设置(尤其是部分代码无法断点问题的解决)
转自http://www.cnblogs.com/worldsing/p/3412323.html
- havok之内存管理
[现象记录] 1.往world和rb里都各自加入一个entityListener,当这个rb被remove掉之后, 会首先调用world里的listener的removecallback, 再调用rb ...