【SpringBoot使用@ConfigurationProperties注解加载多层嵌套配置】

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

配置文件详情

此处以nacos的配置为例做一说明,配置如下:

server:
  port: 8090

spring:
  application:
    name: demo-whq
  profiles:
    active: dev
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      test: test
      test_a: testA
      discovery:
        server-addr: 127.0.0.1:8848,127.0.0.2:8848,127.0.0.3:8848
        namespace: 688400d8-cdae-4bb8-a0b5-18c011ae8cf8    
      config:
        refresh-enabled: true
        name: ${spring.application.name}
        file-extension: yml
        server-addr: 127.0.0.1:8848,127.0.0.2:8848,127.0.0.3:8848
        namespace: 688400d8-cdae-4bb8-a0b5-18c011ae8cf8   
        enabled: true

代码示例

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "spring.cloud.nacos")
public class NacosProperties {
	// 使用构造函数注入
    public NacosProperties(NacosDiscoveryProperties nacosDiscoveryProperties, NacosConfigProperties nacosConfigProperties) {
        this.nacosDiscoveryProperties = nacosDiscoveryProperties;
        this.nacosConfigProperties = nacosConfigProperties;
    }

    private String test;
    private String testA;
    private final NacosDiscoveryProperties nacosDiscoveryProperties;
    private final NacosConfigProperties nacosConfigProperties;
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "spring.cloud.nacos.discovery")
public class NacosDiscoveryProperties {
    private String serverAddr;
    private String namespace;
    private Boolean enabled = true;
    private Boolean registerEnabled = true;
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "spring.cloud.nacos.config")
public class NacosConfigProperties {
    private Boolean refreshEnabled = true;
    private String name;
    private String fileExtension;
    private String serverAddr;
    private String namespace;
    private Boolean enabled;
}

测试验证结果

在这里插入图片描述

总结

网上多数示例中使用的是内部类处理的,个人还是觉得将内部类提出处理更好一点。
对于配置文件中的"_"、"-",创建实体类时直接使用驼峰即可(其他场景未自测)。
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对的,Spring Boot的@ConfigurationProperties注解用于将属性值绑定到bean的属性上。通过@ConfigurationProperties注解,我们可以从配置文件中读取属性值,并将其注入到指定的bean属性中。 要使用@ConfigurationProperties注解,需要按照以下步骤进行操作: 1. 在应用的配置类上添加@ConfigurationProperties注解,并指定要绑定的属性前缀。例如,如果要绑定的属性是以"myapp"开头的,可以在配置类上添加@ConfigurationProperties(prefix = "myapp")注解。 2. 在配置类的属性上添加相应的注解,用于指定要绑定的属性名。常用的注解有@Value、@ConfigurationProperties、@Component等。 3. 在应用的配置文件(一般是application.properties或application.yml)中,添加要绑定的属性和对应的值。属性名需要按照一定规则与配置类属性名对应。 通过以上步骤,我们就可以将配置文件中的属性值绑定到bean的属性上,从而实现配置的灵活性和可管理性。 例如,假设有一个名为MyAppConfig的配置类,其中有一个名为name的属性,我们可以这样使用@ConfigurationProperties注解: ```java @Configuration @ConfigurationProperties(prefix = "myapp") public class MyAppConfig { private String name; // getter和setter方法 // 其他属性 } ``` 在应用的配置文件(application.properties或application.yml)中,添加如下配置: ```properties myapp.name=My Application ``` 这样,在应用启动时,Spring Boot会自动将配置文件中的属性值注入到MyAppConfig的name属性中。 希望以上解答对你有所帮助,如果还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值