首先写一个类:
package com.gb.pay.common.schedulers; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component /** * * @author www.hellojava.com * */ public class CommonJob { @Scheduled(cron="0/3 * * * * ?") public void clearCache(){ System.out.println("job ....."); } }
在启动类里加入
@EnableScheduling
变成
package start; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; import org.springframework.scheduling.annotation.EnableScheduling; @ImportResource(locations={"classpath:dubbo-provider.xml"}) @SpringBootApplication @EnableAutoConfiguration @EnableScheduling @ComponentScan(basePackages={"com.hellojava.pay"}) public class Start { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(Start.class, args); System.out.println("start success"); } }
然后启动Start类,就会自动执行了