四种在application类启动时调用方法
- @PostConstruct 缺点时可能导致依赖没有注入完就执行
@PostConstruct
public void show(){
System.out.println("++++RunTime_@PostConstruct++++");
}
- ApplicationRunner 继承Application接口并覆写run方法 (该类需要被spring托管)
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("++++RunTime_ApplicationRunner++++");
}
- CommandLineRunner 继承CommandLine接口并覆写run方法
@Bean
public CommandLineRunner commandLineRunner(){
return (String... args) -> {
System.out.println("++++RunTime_CommandLineRunner++++");
};
}
- 在一个被spring管理的容器中写static代码块
static {
System.out.println("++++RunTime_Static++++");
}