spring-boot ---Scheduled 定时任务

package com.shi.snyc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 *
 *  @EnableScheduling 开启定时任务注解
 */

@EnableScheduling
@SpringBootApplication
public class SnycApplication {

	public static void main(String[] args) {
		SpringApplication.run(SnycApplication.class, args);
	}
}
package com.shi.snyc.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduledService {

    /**
     * @Scheduled 标注该方法是定时执行的
     * 
     * econd(秒), minute(分), hour(时), day of month (日),month(月) ,day of week (周几)
     * 0 * * * * MON-FRI
     */

    @Scheduled(cron = "0/4 * * * * MON-SAT")  //每隔4秒执行一次
    public  void hello(){
        System.out.println("hello....");
    }


}
#可以直接在类上面标注该注解
@Component
@EnableScheduling


#在方法上面标注下面的注解
@Scheduled(cron="0 59 * * * *")
@Transactional

猜你喜欢

转载自my.oschina.net/u/3677987/blog/2413902