只想說,spring注解scheduled實現定時任務使用真的非常簡單。
一、配置spring.xml文件
1、在beans加入xmlns:task="http://www.springframework.org/schema/task"以及在xsi:schemaLocation中加入
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
2、task任務掃描注解<task:annotation-driven />
3、配置掃描的位置 <context:component-scan base-package="com.test"/>
二、java代碼的實現
1 @Component// 實現定時任務的類必須被@Component注解 2 public class TestScheduled { 3 4 @Scheduled(cron = "0 1 * * * ? ") 5 public void test() {// 定時器的任務方法不能有返回值 6 System.out.println("每分鍾執行一次"); 7 } 8 }
三、運行查看結果
當然是每分鍾打印一次嘍!
是不是很簡單的呀