接着上一篇,这里使用spring配置文件的方式生成spring定时任务。

1、相应的web.xml没有什么变化,因此便不再罗列。同样的,相应的java代码业务逻辑改动也不大,只是在原来的基础上去掉@Component和@Scheduled(cron = "0/5 * * * * ?")参数,也就是把这个类和方法变成一个最简单的java类和方法就可以了。

2、既然是配置文件的方式,那么改动大的自然就是pring.xml配置,把原本用注解实现的定时功能放到配置中来,改动后的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task"
    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.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.1.xsd">

    <!-- 指定相应的包 -->
    <context:component-scan base-package="scheduleTest"/>
    <!-- 指定相应的类 -->
    <bean id="scheTest1" class="scheduleTest.ScheduleTest1"/>

     <!-- 指定要定时任务需要执行的业务逻辑的java类和方法 -->
    <bean id="scheTest11" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject"> <ref local="scheTest1" /> </property>
            <property name="targetMethod">
            <!--  要执行的方法名称  -->
            <value>schTest1</value>
        </property>
        <property name="concurrent" value="true" />
    </bean>

    <!--定义触发的时间 -->
    <bean id="scheTest1Cron" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="scheTest11" />
        </property>
        <property name="cronExpression">
            <value>0/5 * * * * ?</value>
        </property>
    </bean>

    <!--触发器 -->
    <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
           <list> <ref local="scheTest1Cron" /> </list>
        </property>
    </bean>

</beans>

3、一开始我没有导入其他的jar包,然后启动报错classNotFound,如下:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean] for bean with name 'scheTest11' defined in class path resource [spring1.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

查了一下后发现这个包没有在spring-context.jar中,而是在spring-context-support.jar中,于是导入这个包。但之后启动继续报错:
java.lang.NoClassDefFoundError: org/quartz/JobDetail

这是说需要quartz这个包,但是没有导入,于是再次导入,启动依旧报错:
java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionException

这个错误曾经见过,我知道是少了spring-tx.jar这个包,于是再次导入,启动之后终于见到想到的结果,五秒执行一次。
至此,使用spring配置文件的方式生成定时任务的功能实现,maven导包最终配置如下:
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.0.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>quartz</groupId>
        <artifactId>quartz</artifactId>
        <version>1.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
  </dependencies>

spring schedule定时任务(二):配置文件的方式的更多相关文章

  1. spring schedule定时任务(一):注解的方式

    我所知道的java定时任务的几种常用方式: 1.spring schedule注解的方式: 2.spring schedule配置文件的方式: 3.java类继承TimerTask: 第一种方式的实现 ...

  2. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  3. spring实现定时任务的两种方式之spring @scheduled注解方式

    1.使用spring的 scheduled使用注解的方式 这种方法的好处是:使用方便,配置少,提高开发效率: 缺点是:如果使用服务器集群部署方式的时候,其自身无法解决定时任务重复执行的问题. 2.首先 ...

  4. Spring学习笔记二:注入方式

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774608.html  我们说,IOC的实现方式是依赖注入,也就是把被依赖对象赋值到依赖对象的成员属性.怎么做 ...

  5. spring实现定时任务的两种方式

    本文为博主原创,未经允许不得转载 项目中要经常事项定时功能,在网上学习了下用spring的定时功能,基本有两种方式,在这里进行简单的总结, 以供后续参考,此篇只做简单的应用. 1.在spring-se ...

  6. 浅谈spring配置定时任务的几种方式

    网上看到好多关于定时任务的讲解,以前只简单使用过注解方式,今天项目中看到基于配置的方式实现定时任务,自己做个总结,作为备忘录吧. 基于注解方式的定时任务 首先spring-mvc.xml的配置文件中添 ...

  7. Spring加载xml配置文件的方式

    梳理Spring的流程 xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplica ...

  8. Spring加载xml配置文件的方式 ApplicationContext

    大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件的呢? ...

  9. Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)

    描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...

随机推荐

  1. Codeforces B. Divisiblity of Differences

    B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...

  2. Vue.js根据列表某列值更新filter

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. Java源码分析系列之HttpServletRequest源码分析

    从源码当中 我们可以 得知,HttpServletRequest其实 实际上 并 不是一个类,它只是一个标准,一个 接口而已,它的 父类是ServletRequest. 认证方式 public int ...

  4. web.xml组件加载顺序

    在配置项目组件的过程中, 了解Tomcat加载组件顺序很有必要. 例如某些框架如Quartz的集群功能需要数据库的支持, 数据库的加载肯定要在框架组件加载之前. 经过查阅和Debug发现, web.x ...

  5. 有标号DAG计数 [容斥原理 子集反演 组合数学 fft]

    有标号DAG计数 题目在COGS上 [HZOI 2015]有标号的DAG计数 I [HZOI 2015] 有标号的DAG计数 II [HZOI 2015]有标号的DAG计数 III I 求n个点的DA ...

  6. BZOJ 1202: [HNOI2005]狡猾的商人 [带权并查集]

    题意: 给出m个区间和,询问是否有区间和和之前给出的矛盾 NOIp之前做过hdu3038..... 带权并查集维护到根的权值和,向左合并 #include <iostream> #incl ...

  7. HDU 4372 Count the Buildings [第一类斯特林数]

    有n(<=2000)栋楼排成一排,高度恰好是1至n且两两不同.现在从左侧看能看到f栋,从右边看能看到b栋,问有多少种可能方案. T组数据, (T<=100000) 自己只想出了用DP搞 发 ...

  8. WPF: WPF 中的 Triggers 和 VisualStateManager

    在之前写的这篇文章 WPF: 只读依赖属性的介绍与实践 中,我们介绍了在 WPF 自定义控件中如何添加只读依赖属性,并且使其结合属性触发器 (Trigger) 来实现对控件样式的改变.事实上,关于触发 ...

  9. git恢复误删除文件

    在git仓库管理下误删除文件一般会分为以下3种情况: 1.手动直接删掉,如选择-右击-删除 这种删除未修改本地仓库[版本库],只修改了工作区,直接git checkout -- fileName即可恢 ...

  10. qt窗口的切换

    思想:在一个窗口类中声明另一继承与Qdialog的类的变量 还有在另一类中parentwidget()函数获取父类窗口,然后将其隐藏.. 窗口1: mywin1.h #ifndef MYWIN1_H ...