4

I have a bean annotated with @ConfigurationProperties where I have the needed properties by the application bound. I have also annotated the class with @RefreshScope so I can refresh any config properties that change at runtime.

A basic version looks like this:

@Component
@RefreshScope
@ConfigurationProperties
public class ServiceProperties {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    } 
}

The getName() method will constantly be called by different threads.

I had a look at this documentation from Spring: https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#refresh-scope

"Refresh scope beans are lazy proxies that initialize when they are used (that is, when a method is called), and the scope acts as a cache of initialized values. To force a bean to re-initialize on the next method call, you must invalidate its cache entry.

The RefreshScope is a bean in the context and has a public refreshAll() method to refresh all beans in the scope by clearing the target cache. The /refresh endpoint exposes this functionality (over HTTP or JMX). To refresh an individual bean by name, there is also a refresh(String) method."

My understanding from the above documentation is that on a call to refresh (through an actuator /actuator/refresh or other means) the bean is re-created and initialized again.

My question is: Is this whole process thread safe? Is care taken to ensure during this bean refresh process that there will not be any race condition among threads trying to access the name attribute in the class above. I did not find any explicit documentation from Spring on this.

ravihr
  • 53
  • 4

0 Answers0