nacos properties配置写@Value注解的数组list类型写法

场景:

今天有这么几个组配置:

复制代码
# 自定义配置 经过网关检测 - 键
custom.gateway-key=xsffssVal89
# 自定义配置 经过网关检测 - 值
custom.gateway-value=7qwe4r823fjas

### 排除 - 必走网关检验的链接
# 用户图片
custom.interceptor.exclude[0]=/user/images/**
custom.interceptor.exclude[1]=/goods/images/**

这个配置写@ConfigurationProperties(prefix = "custom")注解显然是不太好取到custom.interceptor.exclude,于是只能在配置类中写@Value注解,但是怎么都是获取为null或者报错:

复制代码
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.commonConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'custom.interceptor.exclude' in value "${custom.interceptor.exclude}"

解决办法:

在nacos中properties文件数组写法稍有不同,假设你用@Value注解则无法使用写法以上提到的写法,应该改为:

复制代码
# 自定义配置 经过网关检测 - 键
custom.gateway-key=xsffssVal89
# 自定义配置 经过网关检测 - 值
custom.gateway-value=7qwe4r823fjas

### 排除 - 必走网关检验的链接
# 用户图片、用户图片
custom.interceptor.exclude=/user/images/**,/goods/images/**

附带上配置bean:

复制代码
package com.wpj.common.configuration;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import lombok.Data;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取 ncaso 中 common 设置
 */
@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "custom")
public class CommonConfiguration {

    private final Log logger = LogFactory.getLog(this.getClass());
    /**
     * 经过网关检测 - 键
     */
    private String gatewayKey;
    /**
     * 经过网关检测 - 值
     */
    private String gatewayValue;

    /**
     * 排除 - 必走网关检验的链接
     */
    @Value("${custom.interceptor.exclude}")
    private List<String> interceptorExclude = new ArrayList<>();
}

以上是@ConfigurationProperties注解和@Value注解混合使用方式,假如你将配置改为custom.interceptor-exclude也不用加@Value了,附带上配置bean:

复制代码
package com.wpj.gateway.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取某些 gateway 设置
 */
@Component
@RefreshScope
@ConfigurationProperties("spring.cloud.gateway")
public class GatewayConfig {

    private final Log logger = LogFactory.getLog(this.getClass());

    private List<String> excludePath = new ArrayList<>();

    public List<String> getExcludePath() {
        return this.excludePath;
    }

    public void setExcludePath(List<String> paths) {
        this.excludePath = paths;
        if (paths != null && paths.size() > 0 && this.logger.isDebugEnabled()) {
            this.logger.debug("没有设置排除验证gateway请求路径");
        } else {
            this.logger.debug("排除请求路径设置为: " + paths);
        }
    }
}
相关推荐
Thanks_ks5 小时前
SpringBoot 自动化部署实战:CI/CD 整合方案与避坑指南
pipeline·jenkins·springboot·自动化部署·gitlab ci/cd·ci/cd 实战·docker 容器化
保持学习ing4 天前
黑马Java面试笔记之框架篇(Spring、SpringMvc、Springboot)
java·笔记·spring·面试·mvc·mybatis·springboot
怡人蝶梦5 天前
Java大厂后端技术栈故障排查实战:Spring Boot、Redis、Kafka、JVM典型问题与解决方案
java·jvm·redis·elk·kafka·springboot·prometheus
怡人蝶梦6 天前
Java后端技术栈问题排查实战:Spring Boot启动慢、Redis缓存击穿与Kafka消费堆积
java·jvm·redis·kafka·springboot·prometheus
怡人蝶梦6 天前
Spring Boot启动慢?Redis缓存击穿?Kafka消费堆积?——Java后端常见问题排查实战
java·jvm·redis·kafka·springboot·prometheus·microservices
徐子童6 天前
《Spring Cloud Gateway 快速入门:从路由到自定义 Filter 的完整教程》
java·开发语言·spring cloud·nacos·gateway
JAVA坚守者7 天前
Java 对接 Office 365 邮箱全攻略:OAuth2 认证 + JDK8 兼容 + Spring Boot 集成(2025 版)
springboot·oauth2·office365·java 开发·企业级开发·jdk8 兼容
黎黎黎明⁠⁢8 天前
SpringBoot整合Sa-Token:实现RBAC权限模型
java·sa-token·springboot·idea
计算机软件bs辅导8 天前
t009-线上代驾管理系统
毕业设计·软件工程·springboot·大学生·计算机软件·毕设辅导·代驾系统