3. spring版 timer HelloWorld
1. 由于这回需要使用Spring的jar包,所以需要修改pom文件,添加如下依赖:
<!-- Spring framework --><dependency><groupId>org.springframework</groupId><artifactId>spring</artifactId><version>2.5.6</version></dependency>
package com.helloworld.common;public class SayHelloTask{public void sayHello() {System.out.println("HelloWorld");}}
<bean id="sayHelloTask" />
<bean id="schedulerTask" ref="sayHelloTask" /><property name="targetMethod" value="sayHello" /></bean>MethodInvokingTimerTaskFactoryBean 取代了JDK中的TimerTask<bean id="timerTask"ref="schedulerTask" /><property name="delay" value="2000" /><property name="period" value="6000" /></bean>
<bean /></list></property></bean>
public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Scheduler.xml");}}