詳細能夠參考: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