You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

@RefreshScope与@Configuration、@PropertySource配合失效问题排查

Why Your @RefreshScope Isn't Updating the Token Value

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:

  1. Update your watchdog.token value in the proper property source.
  2. Call POST http://localhost:8080/v1/refresh.
  3. Inject MemcacheProperties into a test endpoint and call it to see if the token is updated.

内容的提问来源于stack exchange,提问作者Manas Saxena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:15:56