springboot配置定时任务

  1. 创建springboot项目
  2. 在启动类中添加开启定时任务注解
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @SpringBootApplication
    @EnableScheduling // 开启定时任务注解
    public class App {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SpringApplication.run(App.class, args);
    	}
    
    }
  3. 创建定时任务类ScheduledTasks
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ScheduledTasks {
    
    	@Scheduled(fixedRate = 2000) // fixedRate:表示以秒为单位,表示2秒执行一次,
    	public void addUser(){
    		System.out.println("-------------添加学生成功----------------");
    	}
    }
    
  4. 启动

猜你喜欢

转载自blog.csdn.net/xiaobo5264063/article/details/89667439