spring定时任务的配置式与注解式

在定时任务配置文件中添加内容:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <task:annotation-driven /> <!-- 定时器开关-->

    <bean id="myTaskXml" class="com.spring.task.MyTaskXml"></bean>

    <task:scheduled-tasks>
        <!--
            这里表示的是每隔五秒执行一次
        -->
        <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
        <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>
    </task:scheduled-tasks>

    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.spring.task" />

</beans>

如果是配置式的定时任务:

package com.spring.task;

/**
 * 基于xml的定时器
 * @author hj
 */
public class MyTaskXml {

    public void show(){
        System.out.println("XMl:is show run");
    }

    public void print(){
        System.out.println("XMl:print run");
    }
}

如果是注解式的定时任务:

package com.spring.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 基于注解的定时器
 * @author hj
 */
@Component
public class MyTaskAnnotation {

    /**
     * 定时计算。每天凌晨 01:00 执行一次
     */
    @Scheduled(cron = "0 0 1 * * *")
    public void show(){
        System.out.println("Annotation:is show run");
    }

    /**
     * 心跳更新。启动时执行一次,之后每隔2秒执行一次
     */
    @Scheduled(fixedRate = 1000*2)
    public void print(){
        System.out.println("Annotation:print run");
    }
}

测试:

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");
    }
}
时间: 2024-08-07 14:29:11

spring定时任务的配置式与注解式的相关文章

spring定时任务详解(@Scheduled注解)

在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd (二)启用注解驱动的定时任务 <task:annot

spring定时任务的配置使用

spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> &l

Spring定时任务的配置

spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> &l

spring 定时任务参数配置详解

注:本文摘自<Quartz Cron 触发器 Cron Expression 的格式>http://blog.csdn.net/yefengmeander/article/details/5985064 非常感谢作者!!! Quartz Cron 表达式支持到七个域  :秒 分 时 日 月 周 年(顺序万不可乱!!!) 名称          是否必须             允许值                 特殊字符  秒                  是             

SpringBoot —— AOP注解式拦截与方法规则拦截

AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. SpringBoot中AOP的使用方式主要有两种:注解式拦截与方法规则拦截,具体使用如下文所示. 一.创建一个简单springboot 2.03项目,添加aop依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId

spring(7)--注解式控制器的数据验证、类型转换及格式化

7.1.简介 在编写可视化界面项目时,我们通常需要对数据进行类型转换.验证及格式化. 一.在Spring3之前,我们使用如下架构进行类型转换.验证及格式化: 流程: ①:类型转换:首先调用PropertyEditor的setAsText(String),内部根据需要调用setValue(Object)方法进行设置转换后的值: ②:数据验证:需要显示调用Spring的Validator接口实现进行数据验证: ③:格式化显示:需要调用PropertyEditor的getText进行格式化显示. 使用

spring(6)--注解式控制器

6.1.注解式控制器简介 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类.已经@Deprecated.   二.Spring2.5引入注解式处理器支持,通过@Controller 和 @RequestMapping注解定义我们的处理器类. 并且提供了一组强大的注解: 需要通过处理器映射DefaultAnnotationHandlerMapping和处理器适配器AnnotationMethodHandlerAdapter来开启支持@Controll

关于spring注解式事务

使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation

spring注解式参数校验

很痛苦遇到大量的参数进行校验,在业务中还要抛出异常或者返回异常时的校验信息,在代码中相当冗长,今天我们就来学习spring注解式参数校验. 其实就是:hibernate的validator. 开始啦...... 1.controller的bean加上@Validated就像这样 1 @ApiOperation(value = "用户登录接口", notes = "用户登录") 2 @PostMapping("/userLogin") 3 publ