@ConfigurationProperties 取值为空的问题

本文详细介绍了在Spring Boot中如何正确配置Redisson连接集群节点。当使用驼峰命名法如clusterNodes获取配置值为null时,文章提供了解决方案,即通过创建一个专门的类来接收多层级配置信息,确保了Redisson能成功读取spring.redis.cluster.nodes属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当取 spring.redis.cluster.nodes =47.111 这样多个点的值时,

如果用 驼峰式的clusterNodes来取值为null,但有多层时需要重新创建一个类来接受,才能取到值

@Data
@ConfigurationProperties(prefix = "spring.redis")
public class RedissonConfig {

    private ClusterProperties cluster = new ClusterProperties() ;
    private String password;
    private String database;

    private String clusterNodes;


    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public class ClusterProperties {
        private String nodes;
    }
}

 


                
在使用@ConfigurationProperties注解进行属性绑定时,涉及多层结构的情况下,可以通过创建嵌套类来表示多层结构,并使用@ConfigurationProperties注解进行属性绑定。例如,创建一个嵌套类来表示多层属性结构,并在该类上添加@ConfigurationProperties注解,指定对应的前缀。然后,通过在父类中引入嵌套类并使用@ConfigurationProperties注解进行属性绑定。具体操作步骤如下: 首先,在父类中引入嵌套类,并在该属性上添加@ConfigurationProperties注解,指定对应的前缀。例如: ``` @ConfigurationProperties(prefix = "parent") public class ParentConfig { private ChildConfig child; // Getter and Setter for child ... } ``` 然后,在父类中创建一个嵌套类,表示多层属性结构,并在该类上添加@ConfigurationProperties注解,指定对应的前缀。例如: ``` @Data public class ChildConfig { private String value1; private String value2; // Getter and Setter for value1 and value2 ... } ``` 最后,在引导类中使用@EnableConfigurationProperties注解指定父类,将其注入到Spring容器中。例如: ``` @EnableConfigurationProperties(ParentConfig.class) @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 这样,就可以通过将多层结构的属性注入到父类中,并在应用程序中访问这些属性。例如,使用@Autowired注解将父类注入到其他组件中,并访问其中的属性。 ``` @Component public class MyComponent { @Autowired private ParentConfig parentConfig; public void doSomething() { ChildConfig childConfig = parentConfig.getChild(); String value1 = childConfig.getValue1(); String value2 = childConfig.getValue2(); // Do something with value1 and value2 ... } } ``` 这样,就可以在应用程序中获取到多层结构的属性了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot学习笔记一](https://blog.youkuaiyun.com/weixin_51391532/article/details/127330905)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【学习日记2023.5.30】之 管理端订单模块完善_调用百度地图优化用户端提交订单是否超出配送距离](https://blog.youkuaiyun.com/qq_42575689/article/details/130956400)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值