Spring3.0之Task

详细可以参考:http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/

不过看完之后,尝试运行,很大可能是失败的,因为细节,配置文件,没有提到。

这个新特性,并不是为了取代quartz,只是为了更好的整合,如果需要强大的功能,更加建议使用quartz!

下面来看看,Spring3.0的定时器是如何配置,

1、新建项目

由于时间关系,我对包就不筛选了,都加入,一些是spring依赖的包:

image

 

2、编写配置文件、开启任务配置的开关

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop http://www.springframework.org/schema/aop/spring-aop-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.netease.lee" /><!--需要扫描的包-->
    <context:annotation-config /><!--开启注解-->
    <task:annotation-driven/> <!-- 这句是重点 定时器开关-->

</beans>

 

3、第一种方式xml编码定时器

package com.netease.lee;

import org.springframework.stereotype.Service;

@Service
public class TaskTestOne {

    public void testOnePrint() {
        System.out.println("TestOne测试打印");
    }

}

 

xml配置:

<task:scheduled-tasks>
    <task:scheduled ref="taskTestOne" method="testOnePrint" cron="1/3 * 2-23 * * ?"/>
</task:scheduled-tasks>

 

 

4、第二种方式-注解方式

package com.netease.lee;

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

@Service
public class TaskTestTwo {
    @Scheduled(fixedDelay = 30000)
    public void testTwoPrint() {
        System.out.println("TestTwo测试打印");
    }

}

 

xml无需配置

 

5、编写加载配置文件的类

package com.netease.lee;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ContextFactoryTest {
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("applicationContext.xml");
    }

}

 

 

6、运行效果:

2010-10-15 17:20:44 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a01335: startup date [Fri Oct 15 17:20:44 CST 2010]; root of context hierarchy
2010-10-15 17:20:44 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2010-10-15 17:20:45 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@17a8a02: defining beans [taskTestOne,taskTestTwo,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.scheduling.annotation.internalAsyncAnnotationProcessor,org.springframework.scheduling.annotation.internalScheduledAnnotationProcessor,org.springframework.scheduling.support.MethodInvokingRunnable#0,org.springframework.scheduling.config.ScheduledTaskRegistrar#0]; root of factory hierarchy
TestTwo测试打印
TestOne测试打印
TestOne测试打印
TestOne测试打印

。。。。。。。

 

成功搞定!

延伸阅读,发现问题:

http://forum.springsource.org/archive/index.php/t-84747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值