spring Quartz基于配置文件和注解的实现
2014-05-31 13:07
387 查看
这里仅仅是做简单的记录怎样实现。
一、基于配置文件的实现
①编写须要调度的类
注:里面的注解是后面的是注解的实现中用到的
②设置配置文件spring-quartz.xml
③启动spring容器
二、基于注解的实现
①配置须要调度的类,并加入注解
②加入配置文件
③启动容器,这里通过配置web.xml启动
这里不知道为何採用ClassPathXMLApplicationContext启动时,报错。看两种方式的配置文件不同,预计是解析xml文件的方式不同。(dom/sax)
一、基于配置文件的实现
①编写须要调度的类
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基于配置文件和注解的实现
- spring Quartz基于配置文件实现
- Srping AOP基于注解和配置文件方式实现前通知,后通知,环绕通知,异常通知,最终通知
- spring Quartz基于配置文件和注解的实现
- 彻底摆脱配置文件 五(基于javaagent实现zkconfigutil对程序零侵入)
- 基于配置文件的工厂设计模式实现,并且做到对象的单例,类似于spring的ioc
- 基于ini配置文件实现多维数组数据的按行存储和读取
- 复习struts2之基于XML配置文件实现指定方法的输入校验以及基于XML校验的一些特点
- 给大家分享一下项目中基于springMVC基于注解部分的xml配置文件,很实用
- Java:基于ORM技术的配置文件持久化实现
- 基于XML的配置文件访问接口设计和实现
- “基于关键字匹配的文本过滤系统”配置文件的设计和实现(C/C++源码)
- struts2基于注解且使用拦截器的文件上传配置
- JAVA WEB开发之基于xml配置文件的分页实现(一) 分页配置原理
- 基于XML的配置文件访问接口设计和实现
- 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)
- 使用注解,实现ssh项目中spring配置文件的零配置,约定优于配置
- linux内核配置文件.config,基于s3c2410实现
- 使用struts2-convention-plugin-2.2.1.1.jar插件实现基于注解的配置。
- 基于注解实现Web Service(不写配置)