Java 自动执行任务

实现一:实现ApplicationRunner接口

 

@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("MyApplicationRunner自动执行任务");
    }
}

 

实现二:实现CommandLineRunner接口

@Component
@Order(0)
public class MyCommandRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandRunner自动执行任务");
    }
}

 

 

调节自动任务的顺序加@Order注解 ,值越小优先级越高

 

posted @ 2022-01-25 09:55  王叫兽  阅读(214)  评论(0)    收藏  举报