1亿笤、CommandLineRunner
2右遭、ApplicationRunner
對比:
1)ApplicationRunner可以解析出命令行參數(shù)
2)ApplicationRunner的run方法聲明throws Exception翅娶,拋出異常會終止啟動
3)都可以使用@Order來指定順序等限,并且共享order順序
3、ApplicationListener
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
log.info("MyApplicationListener is started up");
}
}
1)比本文其他的解決方案都后執(zhí)行
2)@Order只與同類型的ApplicationListener來做排序
4、@PostConstruct
1)Spring 在創(chuàng)建完 bean之后 ,啟動之前執(zhí)行
2)可以使用@DependsOn 注解
5、InitializingBean
1)與 @PostConstruct 解決方案非常相似
2)顧名思義「在屬性設(shè)置之后」馁痴,調(diào)用該方法時,該 bean 的所有屬性已經(jīng)被 Spring 填充肺孤。
3)比使用 @PostConstruct 更安全罗晕,因為使用@PostConstruct有可能@Autowired 字段未自動注入;但如果用構(gòu)造函數(shù)注入赠堵,兩者等效