Search code examples
springspring-cloudspring-cloud-configspring-cloud-consul

Listening to context Refreshed in Spring cloud consul config


Spring cloud consul config allows dynamic refresh of properties whenever the property is changed in consul. Is there a way to listen whenever the change happens?

@Component
public class ContextRefreshListener {

@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
    System.out.println("refreshed");
}

@EventListener
public void handleContextStart(ContextStartedEvent event) {
    System.out.println("started");
}


@EventListener
public void handleContextRefresh(ApplicationContextEvent event) {
    System.out.println("context");
}

}

I tried the above three events, but no luck. Is there any way to do listen to events whenever the refresh happens?


Solution

  • I was able to do it by following way

    @EventListener
    public void handleContextStart(EnvironmentChangeEvent event) {
        System.out.println("changed");
        //Use this for getting the version from consul
    
    }