详细能够参考:http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/php
不过看完以后,尝试运行,很大多是失败的,由于细节,配置文件,没有提到。html
这个新特性,并非为了取代quartz,只是为了更好的整合,若是须要强大的功能,更加建议使用quartz!spring
下面来看看,Spring3.0的定时器是如何配置,app
一、新建项目ide
因为时间关系,我对包就不筛选了,都加入,一些是spring依赖的包:测试
二、编写配置文件、开启任务配置的开关ui
<?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">this
<context:component-scan base-package="com.netease.lee" /><!--须要扫描的包-->
<context:annotation-config /><!--开启注解-->
<task:annotation-driven/> <!-- 这句是重点 定时器开关-->编码
</beans>.net
三、第一种方式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>
四、第二种方式-注解方式
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无需配置
五、编写加载配置文件的类
package com.netease.lee;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ContextFactoryTest {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
六、运行效果:
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