Controller method EventListener(ApplicationStartedEvent.class) getting started before config properties are set using PostConstructor

Viewed 28

I need to call a controller method automatically after Spring Boot Starts. Presently I am having a ConfigProperty class which is reading a configuration from server.

@RefreshScope
@Configuration
@Getter
@Setter
@ConfigurationProperties(prefix = "flagconfig")
public class ConfigProperty {
    public boolean flagOne;
   @PostConstruct
    public void postConstruct() {
        logger.info("myPostConstruct: {}@{} {}",
                this.getClass().getSimpleName(), System.identityHashCode(this), this);
    }

}

Controller code

@Autowired
private ConfigProperty configProperty;

 @EventListener(ApplicationStartedEvent.class)
    public void MyMethod() {
        if (configProperty.flagOne()) {
//doSomething
}
logger.info("Operation=MyMethod Status={}",configProperty.flagOne());

Issue : Even if I set flagOne to true in my server config, during deployment it is not reading the true value and printing Operation=MyMethod Status=false.

Can anyone tell me what I am doing wrong. And how can I run the controller method after config has been set.

If I am calling using PostMan it is going inside configProperty.flagOne(). So I concluded that after sometime he ConfigProperty is able to ge the flag value from server.

0 Answers
Related