SpringBoot中使用@ConfigurationProperties加载多层嵌套配置

SpringBoot中使用@ConfigurationProperties加载多层嵌套配置

日常项目开发中,经常遇到使用多级配置的情况,springboot中可以使用 @ConfigurationProperties 来加载。

网上看到有朋友的配置中,把内部类对象直接new出来,这种方法可能也可以成功,本人没试,毕竟使用spring的,再用new的话,总感觉差点意思啊,所以就自己试着配置了一下。
废话不多说,上正文。

先看效果:

配置文件内容:

在这里插入图片描述

程序输出日志:

在这里插入图片描述

从日志可以看出,配置文件中的多级参数,都被成功加载。

再看代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Data
@Configuration
@ConfigurationProperties("yl-conf")
public class YlConf{

	private String port;

	@Autowired
	private YlUrl ylUrl;
	
	@Data
	@Configuration
	@ConfigurationProperties("yl-conf.yl-url")
	public class YlUrl{
		private String loginUrl;
		private String sendUrl;
	}
}

说明

@ConfigurationProperties 加载的配置前缀字符,中间如果有大写字母,需转为小写,并在前面加上中划线-。如我的配置中,类名YlConf,对应的配置为yl-conf,类名YlUrl,对应的配置为yl-url
另外,前缀并不是必须和类名相同,如图:
在这里插入图片描述

注意子级的前缀配置,需带上它的父级名称,如我的配置:

@ConfigurationProperties("yl-conf.yl-url")

如此,OK。

最后

如果子级过多,全使用内部类就不太妥当,可以将内部类放到外面,其他配置不变。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
使用@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.csdn.net/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.csdn.net/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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值