这里仅仅是做简单的记录怎样实现。

一、基于配置文件的实现

①编写须要调度的类

package com.study;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//@Component
public class QuartzJob {
public QuartzJob(){
System.out.println("Quartzjob创建成功");
}
//@Scheduled(cron = "0/1 * * * * ? ")
public void run(){
System.out.println("Quartz运行的任务调度");
}
}

注:里面的注解是后面的是注解的实现中用到的

②设置配置文件spring-quartz.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>
<bean id="quartzJob" class="com.study.QuartzJob"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!--<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>-->
<value>0/1 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 假设将lazy-init='false'那么容器启动就会运行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime" />
</list>
</property>
</bean> </beans>

注意cron表达式,这里配置的是每隔1s运行。

③启动spring容器

package com.study;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) {
System.out.println("启动spring容器");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-quartz.xml");
}
}

二、基于注解的实现

①配置须要调度的类,并加入注解

package com.study;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class QuartzJob {
public QuartzJob(){
System.out.println("Quartzjob创建成功");
}
@Scheduled(cron = "0/1 * * * * ? ")
public void run(){
System.out.println("Quartz运行的任务调度");
}
}

②加入配置文件

<?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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd " >
<task:annotation-driven/>
<context:component-scan base-package="com.study"/>
<context:annotation-config/>
</beans>

③启动容器,这里通过配置web.xml启动

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-quartz2.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

这里不知道为何採用ClassPathXMLApplicationContext启动时,报错。看两种方式的配置文件不同,预计是解析xml文件的方式不同。(dom/sax)

spring Quartz基于配置文件和注解的实现的更多相关文章

  1. Spring AOP基于配置文件的面向方法的切面

    Spring AOP基于配置文件的面向方法的切面 Spring AOP根据执行的时间点可以分为around.before和after几种方式. around为方法前后均执行 before为方法前执行 ...

  2. Spring的AOP配置文件和注解实例解析

    1.1           Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...

  3. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  4. Spring:基于配置文件的创建对象的各种方式

    在Spring3.0之前,Spring主要创建对象的方法是基于配置文件的,即在配置文件中为对象进行注册,并且可以在配置文件当中为对象的字段或者称之为属性值进行赋值,接下来首先介绍基于配置文件的创建对象 ...

  5. [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. Spring Quartz定时器 配置文件详解

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  7. Spring Security基于Java配置

    Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...

  8. 基于spring+quartz的分布式定时任务框架

    问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...

  9. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

随机推荐

  1. 学习mysql replication

  2. oracle赋某表truncate权限

    一.ins用户下创建存储过程 二.mobapp用户下赋权:执行存储过程的权限 三.odso_insert用户下执行存储过程,即可truncate表 查看odso_insert用户拥有的权限 一.ins ...

  3. jquery-实用例子

    一:jquery实现全选取消反选 3元运算:条件?真值:假值 <!DOCTYPE html> <html lang="en"> <head> & ...

  4. Connect to DB2 database in eclipse via jdbc

    https://stackoverflow.com/questions/23801841/connect-to-db2-database-in-eclipse-via-jdbc

  5. NET定时任务组件Hangfire

    开源的.NET定时任务组件Hangfire解析 项目慢慢就要开工了,很多园友都在问这个事情,看来大伙对这事很上心啊,事情需要一步步的来,尽量写出一个我们都满意的项目.以前每次在博客前面都会扯淡一下,不 ...

  6. jQuery选择器--简洁又全面(转)

    原文:http://www.cr173.com/html/21124_1.html 选择器是jQuery的根基,在jQuery中,对事件处理.遍历DOM和Ajax操作都依赖于选择器.jQuery选择器 ...

  7. Lighthouse前端性能优化测试工具

    在前端开发中,对于自己开发的app或者web page性能的好坏,一直是让前端开发很在意的话题.我们需要专业的网站测试工具,让我们知道自己的网页还有哪些需要更为优化的方面,我自己尝试了一款工具:Lig ...

  8. 系统的Drawable(四)-LayerListDrawable

    系统的Drawable(四)-LayerListDrawable 学习自 https://blog.csdn.net/u014695188/article/details/52815444 Layer ...

  9. ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge

    ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge Arduino环境与Linux环境的桥梁——Bridge 在第一章中介绍Arduino Yun硬件的时候提到过,它上面有 ...

  10. bzoj4456: [Zjoi2016]旅行者

    题目链接 bzoj4456: [Zjoi2016]旅行者 题解 网格图,对于图分治,每次从中间切垂直于长的那一边, 对于切边上的点做最短路,合并在图两边的答案. 有点卡常 代码 #include< ...