SpringBoot------定時任務
阿新 • • 發佈:2018-11-08
步驟,如圖所示
1.新增定時任務1業務類
package top.ytheng.demo.task; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; //定時任務業務類 @Component public class TestTask { //兩秒執行一次 @Scheduled(fixedRate=2000) public void sum() { System.out.println("當前時間:" + new Date()); } }
2.新增啟動類
package top.ytheng.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication//等於下面3個 //@SpringBootConfiguration //@EnableAutoConfiguration //@ComponentScan //攔截器用到 @ServletComponentScan //MyBatis用到 @MapperScan("top.ytheng.demo.mapper") //定時使用(開啟定時任務) @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3.右鍵專案Run As啟動,檢視列印日誌即可