java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot启动成功后继续执行

Springboot项目启动成功后可通过五种方式继续执行

作者:如风之夏

本文主要介绍了Springboot项目启动成功后可通过五种方式继续执行,主要包括CommandLineRunner接口,ApplicationRunner接口,ApplicationListener接口,@PostConstruct注解,InitalizingBean接口,感兴趣的可以了解一下
@Component
public class StartRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner====================");
    }
}
@Component
public class StartRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        System.out.println("ApplicationRunner=================");
    }
}
@Component
public class StartListener implements ApplicationListener<ApplicationStartedEvent> {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        System.out.println("ApplicationListener================ApplicationStartedEvent");
    }
}

如果监听的是 ApplicationStartedEvent 事件,则 ApplicationListener 一定会在 CommandLineRunner 和 ApplicationRunner 之前执行;
如果监听的是 ApplicationReadyEvent 事件,则 ApplicationListener 一定会在 CommandLineRunner 和 ApplicationRunner 之后执行;
顺序:
默认是 ApplicationRunner 先执行,如果双方指定了@Order 则按照 @Order的大小顺序执行,小的先执行

@Component
public class StartInit {

    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct===============================");
    }

}
@Component
public class StartSet implements InitializingBean {

    @Override
    public void afterPropertiesSet() {
        System.out.println("InitializingBean====================");
    }

}

到此这篇关于Springboot项目启动成功后可通过五种方式继续执行的文章就介绍到这了,更多相关Springboot启动成功后继续执行内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文