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);
        }
    }
}
相关推荐
zheeez11 小时前
微服务注册中⼼2
java·微服务·nacos·架构
IT学长编程1 天前
计算机毕业设计 教师科研信息管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·教师科研管理系统
孟诸1 天前
计算机专业毕设-校园新闻网站
java·vue·毕业设计·springboot·课程设计
_院长大人_2 天前
SpringBoot 整合docker,执行容器服务
java·docker·springboot
我是小酒2 天前
掌握 Spring:从新手到高手的常见问题汇总
java·后端·spring·springboot
赚钱给孩子买茅台喝4 天前
智能BI项目第一期
java·人工智能·springboot·react
java1234_小锋4 天前
免费分享一套SpringBoot+Vue学生信息管理系统【论文+源码+SQL脚本】,帅呆了~~
java·springboot·java毕业设计·学生信息·java学生信息·springboot学生信息·vue学生信息
蓝染-惣右介4 天前
【若依RuoYi-Vue | 项目实战】帝可得后台管理系统(一)
java·后端·物联网·vue·springboot
张某布响丸辣4 天前
Nginx 负载均衡:优化网站性能与可扩展性的利器
java·运维·nginx·负载均衡·springboot
爱新觉罗15 天前
Mybatis-plus-Generator 3.5.5 自定义模板支持 (DTO/VO 等) 配置
mybatis·springboot