4

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.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
pChip
  • 405
  • 1
  • 6
  • 15
  • 1
    I am not sure about making autoconfigured properties as "refreshable" but you can try if push notifications can work for you . http://cloud.spring.io/spring-cloud-config/1.4.x/single/spring-cloud-config.html#_push_notifications_and_spring_cloud_bus – Indraneel Bende Jun 08 '18 at 04:46
  • 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 Jun 08 '18 at 04:46
  • 1
    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 Jun 08 '18 at 20:26

2 Answers2

1

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.

pChip
  • 405
  • 1
  • 6
  • 15
  • i am still facing the same problem. when i tried to log the new jdbc url, it is the new value in the property file, but datasource is still created with the old jdbc url. Do we need to manually refresh/kill/recreate the datasource bean? – NIkhil Gupta Sep 03 '18 at 19:41
0

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

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219