Misleading "illegal preference value: " ERROR log printed on startup when preference is not specified in the nacos import location

Author: YanzkCreated Sep 22, 2026Updated Sep 22, 2026

我们鼓励使用英文,如果不能直接使用,可以使用翻译软件,您仍旧可以保留中文原文。另外请按照如下要求提交相关信息节省社区维护同学的理解成本,否则该讨论极有可能直接被忽视或关闭。 We recommend using English. If you are non-native English speaker, you can use the translation software. We recommend using English. If you are non-native English speaker, you can use the translation software. In addition, please submit relevant information according to the following requirements to save the understanding cost of community maintenances, otherwise the discussion is very likely to be ignored or closed directly.

Which Component When a nacos import location does not carry the preference query parameter (i.e. the plain default usage spring.config.import=nacos:<dataId>), a misleading ERROR log is printed on every startup:

当 nacos 配置导入地址没有携带 preference 参数(即默认用法 spring.config.import=nacos:<dataId>)时,每次启动都会打印一条误导性的 ERROR 日志:

2026-09-22T16:50:52.317+08:00 ERROR 45776 --- [service-test] [           main] c.a.c.n.c.NacosConfigDataLoader          : illegal preference value: , using default preference: LOCAL

The application still works correctly with the default preference, but the log message is misleading: it looks like the user configured preference wrongly, while in fact preference was never configured at all. This happens for every nacos config resource, on every startup.

功能本身正常(仍使用默认 preference),但该 ERROR 日志会让人误以为 preference 配置出了问题,而实际上用户根本没有配置过 preference。每个 nacos config resource、每次启动都会出现。

Describe the bug A clear and concise description of what the bug is.

Simplest demo N/A — a plain Spring Boot application with the configuration below is enough to reproduce.

To Reproduce

  1. Create a Spring Boot application with spring-cloud-starter-alibaba-nacos-config and the following configuration: 创建一个使用 spring-cloud-starter-alibaba-nacos-config 的应用,配置如下:
yaml
spring:
  cloud:
    nacos:
      server-addr: http://cluster-nacos.default.svc.cluster.local:8848
  config:
    import: nacos:service-test.yaml

Note: there is no ?preference= parameter in the import location and no spring.cloud.nacos.config.preference property. This is the default usage. 注意:import 地址中没有 ?preference= 参数,也没有设置 spring.cloud.nacos.config.preference。这是默认用法。

  1. Start the application. 启动应用。
  2. Observe the ERROR log above during configuration data loading. 在配置加载阶段即可看到上述 ERROR 日志。

Expected behavior When preference is not specified in the location, the default preference (LOCAL, or the value bound from spring.cloud.nacos.config.preference) should be used silently, without any ERROR log.

未指定 preference 时应静默使用默认值(LOCAL,或 spring.cloud.nacos.config.preference 绑定的值),不打印任何 ERROR 日志。

Screenshots N/A (the log output above is the evidence). N/A(上方日志即为证据)。

Additional context Root cause: the two classes disagree on how "preference is absent" is represented. 根因:两个类对"preference 未指定"的表示不一致。

NacosConfigDataLocationResolver#loadConfigDataResources normalizes an absent preference to "": NacosConfigDataLocationResolver#loadConfigDataResources 把"未指定"规范化为 "":

java
String preference = preferenceFor(uri);
if (preference == null) {
    preference = "";
}
...
.setPreference(preference));

preferenceFor(uri) returns null when the location has no preference query parameter, so NacosItemConfig#preference ends up as "".

But NacosConfigDataLoader#getPreference only treats null as "not specified": 而 NacosConfigDataLoader#getPreference 只把 null 当作"未指定":

java
String specificPreference = resource.getConfig().getPreference();
if (specificPreference != null) {
    try {
        preference = ConfigPreference.valueOf(specificPreference.toUpperCase(Locale.ROOT));
    }
    catch (IllegalArgumentException ignore) {
        // illegal preference value, just ignore.
        log.error(String.format(
                "illegal preference value: %s, using default preference: %s",
                specificPreference, preference));
    }
}

Because specificPreference is "" (not null), the != null check never filters out the "not specified" case; ConfigPreference.valueOf("") always throws IllegalArgumentException, and the catch block logs it at ERROR level. So the most common/default configuration always triggers this misleading ERROR. 由于 specificPreference 是 "" 而非 null,!= null 判断无法过滤"未指定"的情况,ConfigPreference.valueOf("") 必然抛 IllegalArgumentException,被 catch 后以 ERROR 级别记录。也就是说最常见的默认配置必然触发这条误导性日志。

This is unrelated to the value of spring.cloud.nacos.server-addr; any address triggers it as long as the import location has no preference parameter. 该问题与 spring.cloud.nacos.server-addr 的取值无关:只要 import location 中没有 preference 参数,任何地址都会触发。

Suggested fix:

Minimal fix (loader side): treat blank as absent: 最小修复(Loader 侧):将空串视为未指定:

java
if (StringUtils.hasText(specificPreference)) {  // org.springframework.util.StringUtils

I can submit a PR with the minimal fix and a test if you like. 若需要,我可以提交 PR(含最小修复与测试)。

Environment

  • Spring Cloud Alibaba: 2025.1.x (verified on branch 2025.1.x, revision 2025.1.0.1-SNAPSHOT)
  • Spring Boot: 4.1.1
  • Spring Cloud: 2025.1.3
  • JDK: 17
  • OS: macOS

Source: alibaba/spring-cloud-alibaba