@RefreshScope is a Spring Cloud annotation that marks a bean to be destroyed and recreated when the application refreshes its configuration. The next time the bean is used, Spring builds a new instance that reads the updated property values, so you can change settings such as @Value fields without restarting the application. A refresh is usually triggered by a POST to the Actuator /actuator/refresh endpoint.
It is most often paired with a central configuration source such as Spring Cloud Config Server, but it works with any property source that can change at runtime.
@RefreshScope is a custom bean scope named refresh. A bean in this scope is wrapped in a lazy proxy (a CGLIB scoped proxy by default), and the real instance is held in a cache. When a refresh happens:
ContextRefresher re-reads the Environment, including external sources such as a config server or changed files, and publishes an EnvironmentChangeEvent with the keys that changed.Because the bean is recreated lazily, nothing is rebuilt until something calls it. Callers hold the proxy, not the instance, so they do not need to know that a refresh took place. For the same reason, the class cannot be final, and the bean should have a way to be constructed again cheaply.
Add the Actuator and the Spring Cloud dependency management to your build, and expose the endpoint:
With Spring Boot 2.4 and later you import a config server through spring.config.import; the old bootstrap file is not needed:
Then annotate the bean that reads the property:
Inject PricingRules into any controller or service as usual:
Change pricing.discount-percent in your config repository (or your property source), then call the endpoint:
The response lists the changed property keys:
The next request to /price now uses the new discount. The endpoint must be a POST; a GET returns an error.
With many instances, calling each one is tedious. Spring Cloud Bus connects them through a message broker, and a single request to /actuator/busrefresh refreshes every instance. On Kubernetes, Spring Cloud Kubernetes can watch ConfigMaps and trigger a refresh for you.
You often do not need @RefreshScope at all. Beans annotated with @ConfigurationProperties are rebound in place when an EnvironmentChangeEvent fires, so their fields update without recreating the bean:
| Approach | Updates on refresh | Best for |
|---|---|---|
@Value in a @RefreshScope bean | Bean is recreated | Beans that compute state from properties in their constructor or @PostConstruct |
@ConfigurationProperties bean | Properties rebound in place | Plain settings holders |
@Value in a normal singleton | Never | Values that must not change |
@RefreshScope can also go on a @Configuration class, which refreshes the @Bean methods inside it. Properties that Spring Boot reads only at startup, such as server.port, cannot be changed this way.
/actuator/refresh changes behaviour, so expose it only to trusted callers with Spring Security.Yes. It reacts to a refresh of the Environment, whatever the source. Any property source that returns new values after a refresh, such as a changed file or environment, can feed it.
The bean is probably not in refresh scope, or another singleton copied the value. Add @RefreshScope to the bean that reads the property and check the response of the endpoint to confirm the key changed.
The environment updates during the call. Each @RefreshScope bean is rebuilt on its next use, so the effect appears on the next request that touches it.
No. It only rebuilds beans in refresh scope. For a full restart you need a separate mechanism such as a rolling deployment.
@RefreshScope lets selected beans pick up changed configuration at runtime by recreating them lazily after a refresh. Expose /actuator/refresh, send a POST, and the next call to each annotated bean uses the new values. Prefer @ConfigurationProperties for simple settings, keep refreshable beans lightweight, use Spring Cloud Bus for fleets, and protect the refresh endpoint.