5

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.

3
  • 1
    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/… Commented Jun 8, 2018 at 4: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. Commented Jun 8, 2018 at 4: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. Commented Jun 8, 2018 at 20:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.