Prevent wrong application props refresh in Spring

152 Views Asked by At

I have Spring Boot app with several configs that have to refresh without app restart. They look like as such:

@Setter
@Getter
@Component
@RefreshScope
@ConfigurationProperties(prefix = "test")
class TestConfigProps {

    private Integer val;
}

All works well - beans are refreshed. But in case of exception during config refresh and bean reinitialization there are infinite exceptions of bean initialization (cause @RefreshScope bean is proxy - they appears lazily). E. g. if we pass string instead of integer:

Error querying consul Key/Values for context 'App/'. Message: Error creating bean with name 'testConfigProps' nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'test.value' to Integer 

On refresh event Spring does destroting and creating new config beans:

public void refreshAll() {
    super.destroy();
    this.context.publishEvent(new RefreshScopeRefreshedEvent());
}

How can I create refreshed bean only in case of valid new properties? And in error value cause leave it the same (RefreshScope.refreshAll)?

0

There are 0 best solutions below