java项目启动时执行指定方法

1. @PostConstruct注解

在Spring加载类的时候执行一次

@PostConstruct
private void test(){

}

2. CommandLineRunner接口

类似于Main方法启动,可以接受一个字符串数组的命令行参数

@Component
public class test implements CommandLineRunner{
@Override
public void test1(String... args) throws Exception{

}

3. ApplicationRunner 接口

这种方式与实现CommandLineRunner接口的区别就是他的参数是ApplicationArguments

@Order(value = 1)
@Component
public class MyApplicationRunner implements ApplicationRunner{
@Override
public void run(ApplicationArguments args) throws Exception{ 
}
}

 

posted @ 2020-12-03 13:57  ___mouM  阅读(404)  评论(0编辑  收藏  举报