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 天前
互联网大厂求职面试记:谢飞机的搞笑答辩
java·jvm·spring·面试·springboot·线程池·多线程
叫我阿柒啊2 天前
从全栈开发到微服务架构:一位Java工程师的实战经验分享
java·ci/cd·kafka·mybatis·vue3·springboot·fullstack
原来是好奇心2 天前
Spring Boot 事务失效的八大原因及解决方案详解
spring·springboot·事务
mask哥2 天前
DP-观察者模式代码详解
java·观察者模式·微服务·设计模式·springboot·设计原则
一包烟电脑面前做一天3 天前
.NetCore下Ocelot + Nacos 实现负载均衡
nacos·负载均衡·.netcore·ocelot·ocelot集成nacos
叫我阿柒啊3 天前
从Java全栈到前端框架:一场真实面试的深度技术探索
java·redis·微服务·typescript·vue3·springboot·jwt
awei09163 天前
SpringBoot3中使用Caffeine缓存组件
java·缓存·springboot·springboot3
麦兜*3 天前
MongoDB 事务管理:多文档操作如何保证 ACID?
java·数据库·后端·mongodb·spring cloud·springboot
叫我阿柒啊4 天前
从Java全栈开发到微服务架构:一次真实的面试实录
java·微服务·vue3·springboot·前端开发·后端开发·项目经验