4
votes

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.

2
I am not sure about making autoconfigured properties as "refreshable" but you can try if push notifications can work for you . cloud.spring.io/spring-cloud-config/1.4.x/single/… - Indraneel Bende
If the above thing does not work , can you make a feature request on spring cloud config's github page. This could be a good feature. - Indraneel Bende
Actually, both the push notifications, and a request to the /actuator/refresh endpoint did refresh the datasource properties and re-create the db connection. See my answer below for more details. - pChip

2 Answers

1
votes

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.

0
votes

There's a configuration property to set in case of the Autoconfigured bean is immutable (don't change the properties after initialized)

You can put a list (set) of classes that you need to be refreshed and you don't have control over the source code, you can put them under the property: spring.cloud.refresh.extra-refreshable

e.g.:

spring
  cloud
    refresh
      extra-refreshable:
        - org.springframework.mail.javamail.JavaMailSenderImpl

see: https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html#refresh-scope