Search code examples
springspring-bootspring-cloud-configspring-config

Refresh Spring Boot Auto-Configured Properties


Can I dynamically refresh properties that are used by Spring Boot's auto configuration setup?

For example, I have the following properties set (via cloud config) to auto configure a dataSource:

spring.datasource.username=user1
spring.datasource.password=test

Now if I change the password prop on the config server, and hit the /refresh endpoint, I can see that the updated prop is retrieved but the DataSource is not refreshed.

I know I can manually configure the DataSource beans and make sure they fall under a RefreshScope, but I was hoping to find a way to mark the auto configured properties as "refreshable". I have some use cases where I'd want to refresh props used by Spring Boot for other beans besides DataSources, and setting up some of those beans manually could be a pain.


Solution

  • I think I spoke too soon, at least as far as my DataSource example goes. A new db connection was being created with the updated props.

    Which makes sense especially when looking at the docs here

    This didn't re-connect some of my spring.cloud.stream.bindings properties I had, but in that case I can probably solve the issue with @RefreshScope.