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);
        }
    }
}
相关推荐
邂逅星河浪漫1 天前
【RabbitMQ】消息队列·详解+实操演示+功能实现(微服务架构)
rabbitmq·springboot·springamqp·consumer·exchange·producer·queue
vortex53 天前
用 Scoop 快速部署 JeecgBoot 开发环境:从依赖安装到服务管理
java·windows·springboot·web·开发·jeecg-boot
代码哈士奇3 天前
Nestjs+nacos+kafka搭建中后台系统-后端(持续更新中)
redis·分布式·微服务·nacos·kafka·nestjs·pgsql
javpy6 天前
为什么Service层和Mapper层需要实现interface接口
java·springboot
bxlj_jcj6 天前
Nacos注册中心:从服务注册到负载均衡
spring cloud·nacos
阿里-于怀7 天前
行业首发!Spring AI Alibaba + Nacos 支持分布式 Multi-Agent 构建
人工智能·分布式·ai·nacos·saa·multi agent
古渡蓝按7 天前
基于 Word 模板占位符的动态文档生成实践(源码+保姆版)
springboot
带刺的坐椅7 天前
Solon (可替换 SpringBoot)集成 Docker 实战:30分钟搞定轻量级应用容器化部署
java·docker·jar·springboot·solon
代码哈士奇7 天前
简单使用Nest+Nacos+Kafka实现微服务
后端·微服务·nacos·kafka·nestjs
程序员杨工7 天前
【原创】SpringBoot3+Vue3客户管理系统
vue.js·springboot