Skip to content

[ISSUE #361] support handling deleted config in @NacosValue#362

Open
zmz789 wants to merge 1 commit intonacos-group:developfrom
zmz789:config-del-reset
Open

[ISSUE #361] support handling deleted config in @NacosValue#362
zmz789 wants to merge 1 commit intonacos-group:developfrom
zmz789:config-del-reset

Conversation

@zmz789
Copy link

@zmz789 zmz789 commented Mar 10, 2026

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

当前 @NacosValue 在 Nacos 配置被删除时,会保留历史值不变,导致应用无法感知配置已删除,继续使用过期的配置值,可能引发业务逻辑错误。本改动通过新增 nacosValue.resetOnConfigDelete 配置项,提供了一种可选的重置行为:当配置被删除时,优先使用用户在注解中定义的默认值(如 ${value:500} 中的500),若无用户默认值则回退到类型默认值。这样既保证了向后兼容(默认行为不变),又为需要感知配置删除的场景提供了明确的重置机制,避免应用在配置删除后继续使用过期值。

Brief changelog

  • 新增 nacosValue.resetOnConfigDelete 配置项,支持通过 JVM 系统属性(-D)或 Spring Environment(application.properties)控制配置删除时的行为
  • 当 nacosValue.resetOnConfigDelete=true 时,Nacos 配置被删除后,@NacosValue 注入的字段会重置为用户自定义默认值,若无用户默认值则回退到类型默认值(基本类型为 0/false 等,非基本类型为 null)
  • 当 nacosValue.resetOnConfigDelete=false(默认)时,保持原有行为:配置删除后保留历史值,不做任何变更

Verifying this change

  1. 编写测试用例,使用 @NacosValue(value = "${test.key:500}", autoRefreshed = true) 注入一个 int 类型字段
  2. 启动应用,确认 Nacos 配置中心存在 test.key 配置项时,字段值为配置值
  3. 在 Nacos 控制台删除 test.key 配置项
  4. 设置 nacosValue.resetOnConfigDelete=true(通过 JVM 参数 -DnacosValue.resetOnConfigDelete=true 或在 application.properties
    中配置),确认字段值重置为 500(用户自定义默认值)
  5. 对于没有用户默认值的表达式(如 ${test.key}),确认基本类型字段重置为类型默认值(如 int 为 0),非基本类型字段重置为 null
  6. 设置 nacosValue.resetOnConfigDelete=false(或不设置,使用默认值),确认配置删除后字段值保持不变(保留历史值)
  7. 验证 nacosValue.resetOnConfigDelete 的优先级:JVM 系统属性优先于 Spring Environment 配置

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

@CLAassistant
Copy link

CLAassistant commented Mar 10, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants