@Component
public class initConfig {
//1、静态代码块
static {
System.out.println("aaaaa");
System.out.println("===================");
}//2、构造方法
public initConfig(){
System.out.println("bbbbbb");
System.out.println("===================");
}
//3、注解
@PostConstruct
public void init(){
System.out.println("ccccccc");
System.out.println("===================");
}
}
@Component
@Order(1)//执行顺序
public class TestApplicationRunner implements ApplicationRunner{
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
System.out.println("order1:TestApplicationRunner");
}
}
@Component
@Order(2)
public class TestCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
System.out.println("order2:TestCommandLineRunner");
}
}
文章图片
【springBoot启动时自动执行代码的几种方式】总结:
加载顺序为static>constructer>@PostConstruct>CommandLineRunner和ApplicationRunner
推荐阅读
- 为什么我复制文件比你快
- 如何修复Java错误代码1618(解决办法介绍)
- Java|布隆过滤器
- 深度学习|TensorFlow惊现大bug(网友(这是逼着我们用PyTorch啊!))
- java|IT界惊现文豪!华为领导及阿里P10遭吐槽
- java|突发!Spring Cloud 爆高危漏洞。。赶紧修复!!
- Java URLConnection类
- Java URL
- 了解javap工具