Calling A Method After all SpringBeans and ApplicationContext have been initialized

前端 未结 4 602
滥情空心
滥情空心 2020-12-18 21:22

I have a method in a complex java program that needs to be called immediately after the web ApplicationContext and SpringBeans have been initialized.

I\'ve tried toy

4条回答
  •  悲&欢浪女
    2020-12-18 21:49

    In Spring 4.2 onwards you can attach event listeners to Springs Lifecycle events (and your own) using annotations. Simple add the @EventListener to a method and include the event type as the first (and only) parameter and Spring will automatically detect it and wire it up.

    https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2

    @Component
    public class MyListener {
    
        @EventListener
        public void handleContextRefresh(ContextRefreshedEvent event) {
            ...
        }
    }
    

提交回复
热议问题