spring Quartz基于配置文件和注解的实现
这里仅仅是做简单的记录怎样实现。
一、基于配置文件的实现
①编写须要调度的类
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.xsdhttp://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基于配置文件和注解的实现相关推荐
- SSM框架笔记07:初探Spring——采用XML配置文件与注解方式
初探Spring--采用XML配置文件与注解方式 在上一讲的项目基础上继续. 1.将xmlconfig包里的两个骑士类和两个任务类拷贝到xml_annotation包 2.修改SlayDrag ...
- 从源码深处体验Spring核心技术--基于注解的IOC初始化
Annotation 的前世今生 从 Spring2.0 以后的版本中,Spring 也引入了基于注解(Annotation)方式的配置,注解(Annotation)是 JDK1.5 中引入的一个新特 ...
- 基于XML和注解的Spring Bean管理
文章目录 Spring工厂接口 BeanFactory接口 ApplicationContext 接口 Spring的bean管理的两种方式: 3 种实例化bean的方式(xml) 通过构造方法创建b ...
- 基于spring+quartz的分布式定时任务框架
http://www.cnblogs.com/aaronfeng/p/5537177.html 问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比 ...
- java @around,Spring AOP基于注解的Around通知
是一种建议类型,可确保方法执行前后的通知可以运行. 以下是通知的语法: 语法 @Pointcut("execution(* com.yiibai.Student.getAge(..))&qu ...
- spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现
spring 定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现 依赖包 如下 <dependencies><dependen ...
- Spring :读取配置文件(.properties、.yam)相关注解
1.美图 2.概述 读取配置文件相关的注解,就是用来帮助我们获取到配置文件.properties..yam里面信息的. 读取配置文件相关注解 解释 @EnableConfigurationProper ...
- Spring MCV基于注解的控制器
一 Controller类的实现 package org.fkit.controller; import org.springframework.stereotype.Controller; impo ...
- Spring+Quartz定时任务调度
Spring+Quartz定时任务调度 2011/5 zqhxuyuan@gmail.com 参考文章: http://www.iteye.com/topic/399980 org.sprin ...
- spring 加载java类_在Spring中基于Java类进行配置的完整步骤
在Spring中基于Java类进行配置的完整步骤 发布于 2020-7-7| 复制链接 基于Java配置选项,可以编写大多数的Spring不用配置XML,下面 前言JavaConfig 原来是 Spr ...
最新文章
- flex和bison实例分析
- Fedora 12 环境搭建
- 到底是Java好还是Python好?
- HTML基础第四讲---图像
- 最小花费(最短路变形+中南大学复试机试)
- windows下mysql8.x配置远程连接
- LETTERS (信息学奥赛一本通-T1212)
- python中的ture是常量吗_python中的true是什么
- python D9 初识函数
- ssh登录慢的解决办法
- Qt 局域网聊天工具
- 软路由服务器安装在哪个位置,软路由安装教程_软路由安装注意事项
- Elasticsearch学习之的delimited_payloads使用
- 什么情况下使用$set?
- 计算机经典书籍- -
- 深入浅出pytorch
- 2021年电赛E题解析数字模拟信号混合传输收发机(详细介绍)
- 程序员做什么副业最轻松最赚钱?
- JAVA Json数据转换实体对象
- 计量经济学及Stata应用 第五章习题 5.5