Home > AI > Backend > SpringBoot >

 ContextRefreshedEvent

On either initializing or refreshing the ApplicationContextSpring raises the ContextRefreshedEvent. Typically a refresh can get triggered multiple times as long as the context has not been closed.

Notice that, we can also have the event triggered manually by calling the refresh() method on the ConfigurableApplicationContext interface.

Method 1 to handle this event

@Component
public class MyBean implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("Shark-ContextRefreshedEvent");

    }
}

Method 2:

Component
class MyBean{

    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        System.out.println("Shark-There you go-ContextRefreshedEvent");
    }
}

Leave a Reply