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);
        }
    }
}
相关推荐
caihuayuan417 小时前
react拖曳组件react-dnd的简单封装使用
sql·spring·vue·springboot·课程设计
工一木子21 小时前
【工具使用】IDEA 社区版如何创建 Spring Boot 项目(详细教程)
springboot·idea
源码姑娘1 天前
基于DeepSeek的智慧医药系统(源码+部署教程)
java·人工智能·程序人生·毕业设计·springboot·健康医疗·课程设计
程序员小白条2 天前
【大学生体质】智能 AI 旅游推荐平台(Vue+SpringBoot3)-完整部署教程
java·程序员·vue·springboot·毕设·管理系统·课设
程序猿000001号2 天前
Java实现Consul/Nacos根据GPU型号、显存余量执行负载均衡
nacos·负载均衡
Kale又菜又爱玩5 天前
Logback:高性能日志框架完全指南
java·springboot·logback
Jing_saveSlave6 天前
基于 Spring Boot 的企业级快速启动模板 —— spring-quick
spring boot·后端·spring·springboot·敏捷开发·脚手架·企业级
LUCIAZZZ7 天前
ThreadLocal的Key是弱引用给垃圾回收带来的问题
java·开发语言·jvm·spring·springboot
非 白11 天前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
放学-别走12 天前
基于springboot 以及vue前后端分离架构的求职招聘系统设计与实现
java·mysql·spring·vue·毕业设计·springboot·毕设