spring3.0定时器 xml配置和注解方式


1.xml配置方式



web.xml

<!-- 配置spring监听器和配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

spring配置文件

     
     <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:task="http://www.springframework.org/schema/task"
     xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
    
    <task:scheduled-tasks>   

        <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   

    </task:scheduled-tasks> 

    <context:component-scan base-package="com.gy.mytask" />

这里定时每分钟的的零秒开始执行

cron="0 * * * * ?"

java类,方法无返回值

package com.gy.mytask;

import org.springframework.stereotype.Service;

@Service

public class TaskJob {

    public void job1() {

        System.out.println("任务进行中。。。");
        System.out.println(Thread.currentThread().getName());

    }

}


2.注解方式


spring配置文件

    
    
     <context:annotation-config />  <!-- 可以去掉 -->
     
    <context:component-scan base-package="com.gy.mytask" />
    
    <!-- 开启这个配置,spring才能识别@Scheduled注解 -->     

    <task:annotation-driven />


<context:annotation-config/>

作用是显式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor。注册这4 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。


java类

package com.gy.mytask;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Service;


@Service  

public class TaskJob {
    @Scheduled(cron = "0 * * * * ?")
    public void job1() {

        System.out.println("任务进行中。。。");

    }

}



默认是单线程的

pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1


参考文章:

http://my.oschina.net/u/559635/blog/389558

http://blog.sina.com.cn/s/blog_872758480100wtfh.html


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1741155

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值