Spring boot 通过 @ConfigurationProperties获取属性

假设配置文件的属性是这样的。。

spring:
  cloud:
    consul:
      config:
        enabled: true              #enabled 设置config是否启用,默认为true            
        format: yaml               #format 设置配置的值的格式,可以yaml和properties             
        prefix: config             # prefix 设置配的基本目录,比如config              
        defaultContext: schedule   # defaultContext 设置默认的配置,被所有的应用读取  
        data-key: data
                                    # profiles是支持springboot多环境

Java代码解析

@ConfigurationProperties(prefix = "spring")
@Configuration
@ToString
@Data
public class SpringConfiguration{
    private CloudConfiguration cloud = new CloudConfiguration();

}

@Data
public static class CloudConfiguration{
    private ConfigConfiguration config = new ConfigConfiguration();
}

@Data
public static class ConfigConfiguration{
    private Boolean false;

    private String format;

    private String prefix;

    private String defaultContext;

}

注意.yml中配置名首个配置不能为驼峰

`@ConfigurationProperties` 是 Spring 框架提供的用于外部化配置的一种机制,它能将应用程序所需的配置项从属性文件中自动绑定到对应的 Java 对象上。这使得管理复杂的设置变得简单易行,并有助于维护良好的编码实践。 当你编写单元测试时,为了确保 `@ConfigurationProperties` 的功能正常工作以及验证其默认值、数据校验等特性是否正确无误地被应用到了目标 Bean 上面,你需要做以下几件事情: 1. **创建一个包含 `@ConfigurationProperties` 注解的类**:此步骤通常已经完成于主程序代码里,例如下面的例子展示了一个简单的配置模型: ```java import org.springframework.boot.context.properties.ConfigurationProperties; @Data // Lombok annotation for getters and setters. @AllArgsConstructor @NoArgsConstructor @ConfigurationProperties(prefix = "example") public class ExampleConfig { private String propertyOne; private int propertyTwo; } ``` 2. **编写相应的单元测试案例**:你可以通过多种方式来进行这项任务;这里给出了一种基于 JUnit 和 Mockito 的方法示例(假设你已经在项目中包含了这些依赖): - 使用 `@SpringBootTest(classes = {ExampleConfig.class})` 或者更轻量级的方式如 `@ExtendWith(SpringExtension.class)` 来启动上下文环境; - 利用 `MockitoAnnotations.initMocks(this);` 初始化 mock 实体 (如果有的话); - 创建一个临时的应用配置文件(`application-test.yml`)并指定给你的测试运行器,该文件应该位于 src/test/resources 目录下并且内容类似于这样的形式: ``` example.property-one: testValueForPropertyOne example.property-two: 4567890 ``` 3. **在测试函数内部获取注入了 `@ConfigurationProperties` 标签的对象实例** : 可以直接利用 `@Autowired` 进行自动化装配 ```java import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @SpringBootTest @ActiveProfiles("test") public Tests { @Autowired private ExampleConfig config; @Test void testLoadFromYaml() throws Exception{ assertEquals("testValueForPropertyOne",config.getPropertyOne()); } ``` 以上就是一个关于如何对带有 `@ConfigurationProperties` 注释的组件进行单元测试的基本指导。当然,在实际操作过程中还需要考虑更多因素比如异常处理、边界条件等等,但上述流程足以让你开始尝试着去理解这一过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值