Spring Task 組件使用二

第一步:修改applicationContext配置文件java

<!-- 添加一下項 -->
<task:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="group.esperanto"/>

第二步:定義執行類spring

package group.esperanto.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component   //必須標記爲一個Spring管理的JavaBean類
public class MyTask3 {   //不須要繼承任何子類
	//@Scheduled(fixedRate=2000) //設置爲兩秒一執行   間隔觸發
	@Scheduled(cron="* * * * * ?" )  // 一秒一執行    定時觸發
   public void excJob() {
	   System.out.println("當前日期:  " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
   }
}
相關文章
相關標籤/搜索