@RefreshScope与@Configuration、@PropertySource配合失效问题排查
Let's walk through the common issues here and how to fix them:
1. @PropertySource Doesn't Support Dynamic Refresh Out of the Box
The @PropertySource annotation loads your application.properties file once at application startup. When you modify the file later and call /v1/refresh, Spring doesn't automatically re-read the local properties file. That means the value in the Spring Environment is still the old one—so your @Value injection can't pick up the new token.
2. Static Variables Break Refresh Scope (If You're Using Them)
If your MemcacheProperties class is storing the token in a static variable, even with @RefreshScope, the static value won't update when the bean is recreated. Static variables are tied to the class, not individual bean instances, so they ignore scope refreshes entirely.
3. @Value + @RefreshScope Works Best with Refreshable Property Sources
@Value relies on the Spring Environment for values. If the property source (your local application.properties) isn't set up to be refreshable, the Environment never gets the new value, even after calling /refresh.
Fixes to Try
Fix 1: Use @ConfigurationProperties (Recommended)
Swap @Value for @ConfigurationProperties—it's designed to work seamlessly with @RefreshScope and handles property binding more reliably for dynamic updates. Here's how to adjust your code:
@Configuration @RefreshScope @ConfigurationProperties(prefix = "watchdog") public class MemcacheProperties { private String token; // Getter and setter are required for ConfigurationProperties public String getToken() { return token; } public void setToken(String token) { this.token = token; } }
This way, when you trigger a refresh, Spring will re-bind the watchdog.token value from the Environment to your properties class automatically.
Fix 2: Avoid Static Variables in Your Configuration Class
If you want to stick with @Value, make sure your token is an instance variable, not static:
@Configuration @RefreshScope @PropertySource("classpath:application.properties") public class MemcacheProperties { @Value("${watchdog.token}") private String token; // Instance variable, not static public String getToken() { return token; } }
Static variables won't update when the @RefreshScope bean is recreated, so this is a critical fix if you're using them.
Fix 3: Use a Refreshable Property Source (Like Spring Cloud Config)
Local application.properties files aren't meant for dynamic updates in production. For proper refreshable configuration, use Spring Cloud Config Server to host your properties. When you update the config in the server and call /refresh, Spring will pull the new value automatically.
If you need to use a local file for testing, run your app with an external properties file (so Spring can detect changes):
java -jar your-app.jar --spring.config.location=file:/path/to/your/application.properties
Then, manually call /refresh after modifying the file to trigger an update.
Fix 4: Verify Actuator Setup
Double-check that you have the actuator dependencies and the refresh endpoint enabled in your application.properties:
# Add these if missing management.endpoints.web.exposure.include=refresh spring.cloud.refresh.enabled=true
Quick Test to Confirm
After making these changes:
- Update your
watchdog.tokenvalue in the proper property source. - Call
POST http://localhost:8080/v1/refresh. - Inject
MemcachePropertiesinto a test endpoint and call it to see if the token is updated.
内容的提问来源于stack exchange,提问作者Manas Saxena

