接着上一篇,这里使用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. Tomcat8远程访问manager,host-manager被拒绝403

    Tomcat部署在服务器之后在服务器本地访问manager和host-manager成功(即127.0.0.1:8080或者localhost:8080),但使用测试主机访问tomcat的manage ...

  2. 服务器开发之CGI后门

    1.html代码 <form id = "form" name="form" method="post" action=". ...

  3. C#基础(七)虚函数

    若一个实例方法声明前带有virtual关键字,那么这个方法就是虚方法.虚方法与非虚方法的最大不同是,虚方法的实现可以由派生类所取代,这种取代是通过方法的重写实现的(以后再讲)虚方法的特点:虚方法前不允 ...

  4. 创建分模块的maven项目

    折腾了我2天的maven,整理一下,以后做个参考 一.什么是maven项目: Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven ...

  5. CRM项目-1模型与站点管理

    一.项目环境 语言:python3 IDE:pycharm 组件:bootstarp,jQuery 二.模型 2.1编写模型 联合唯一 class Meta:显示中文名. 认证使用django自带的U ...

  6. JasperReport报表开发(一)--原理介绍

    1. JasperReport介绍 JasperReport 是一个开源的Java报表引擎,它不像其他的报表工具,例如Crystal报表是基于Java的,没有自己的表达式语法.Jasper Repor ...

  7. dos2unix和unix2dos

    dos2unix将windows格式的文件转换为linux格式的文件. unix2dos将linux格式的文件转换为windows格式的文件. dos2unix和unix2dos会转换windows和 ...

  8. 【linux之文件查看,操作】

    一.shell如何处理命令 1.shell会根据在命令中出现的空格字符,将命令划分为多个部分 2.判断第一个字段是内部命令还是外部命令 内部命令:内置于shell的命令(shell builtin) ...

  9. 洛谷 [P2483] [模板] k短路

    人生中的第一道黑题... 其实就是k短路模板 #include <iostream> #include <cstdio> #include <cstring> #i ...

  10. 「POJ2505」A multiplication game [博弈论]

    题目链接:http://poj.org/problem?id=2505 题目大意: 两个人轮流玩游戏,Stan先手,数字 p从1开始,Stan乘以一个2-9的数,然后Ollie再乘以一个2-9的数,直 ...