赶上spring初始化所有bean的时刻


问题内容

我有spring申请表(我没有懒豆)。

我想在所有@Component(@Repositoey @Service @Controller)Bean初始化时插入逻辑。

我该怎么做?


问题答案:

如对这个问题的回答所述,您可以使用ApplicationListener并查找ContextRefreshedEvent

public class Loader implements ApplicationListener<ContextRefreshedEvent>{

        public void onApplicationEvent(ContextRefreshedEvent event) {
                 // whatever you want to do when app context is initialized or refreshed
        }
}