一 postConstruct 注解
二 实现 InitializingBean 接口 重写afterProperties 方法
三 实现ApplicationListener重写onApplicationEvent 方法
监听ContextRefreshedEvent事件。
在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理。
event.getApplicationContext().getParent() == null
最后测试了一下执行顺序



