Springboot中使用@ConfigurationProperties读取yml配置文件数据及注意事项

配置文件数据:

person:
  name: linzhi
  age: 18

读取配置文件数据:

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

/**
 * @ConfigurationProperties:类中属性和yml配置文件中键值一一映射
 * prefix = "person":声明配置文件中哪个键值进行一一映射
 * @Component:把类添加到容器里
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

单元测试:

@SpringBootTest
class Sbdemo01ApplicationTests {

    //@Autowired:使用的时候要自动加载,不然值会为null
    @Autowired
    Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }
    
}

注意事项:

PS:读取的是yml配置文件,配置数据必须放在application.yml中,否则读不到

1、【映射读取类】的getter、setter方法要记得添加

2、【映射读取类】的注解要记得添加

(1)@Component    //添加到容器中, @ConfigurationProperties(prefix = "person")注解的功能才能生效

(2) @ConfigurationProperties(prefix = "person")     //声明类与配置文件的映射关系

3、使用【映射读取类】时需要添加 @Autowired 注解进行自动从容器中加载,不加的话读取到的数据为null

4、pom.xml添加以下配置文件处理器依赖,设置配置文件数据会有提示:

        <!--配置文件处理器:配置文件与类进行绑定会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

提示:

### Spring Boot使用 `@ConfigurationProperties` 注解读取配置文件 #### 创建配置类 为了使 `@ConfigurationProperties` 正常工作,需创建一个Java类用于映射属性文件中的键值对。该类应包含与配置项对应的字段以及相应的getter和setter方法。 ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "myapp") public class MyProperties { private String accessKey; private String secretKey; // Getters and Setters public String getAccessKey() { return accessKey; } public void setAccessKey(String accessKey) { this.accessKey = accessKey; } public String getSecretKey() { return secretKey; } public void setSecretKey(String secretKey) { this.secretKey = secretKey; } } ``` 此代码片段展示了如何定义一个名为 `MyProperties` 的组件,并通过指定前缀 `prefix="myapp"` 来关联特定的配置条目[^2]。 #### 编写 application.properties 文件 确保在项目的资源路径下存在 `application.properties` 或者其他形式的应用程序配置文件(如 `.yml`),并将必要的设置添加进去: ```properties # src/main/resources/application.properties myapp.accessKey=your-access-key-here myapp.secretKey=your-secret-key-here ``` 上述配置指定了应用程序所需的访问密钥和秘密密钥[^3]。 #### 测试配置加载情况 最后一步是验证这些配置能否被成功解析到对象实例中。可以借助于 JUnit 和 Spring Boot 提供的支持来进行单元测试。 ```java @SpringBootTest class PropertiesTest { @Autowired private MyProperties myProperties; @Test void contextLoads() { System.out.println("配置accessKey: " + myProperties.getAccessKey()); System.out.println("配置secretKey: " + myProperties.getSecretKey()); } } ``` 这段测试代码会打印出从配置文件中获取到的实际值,从而证明了 `@ConfigurationProperties` 已经正确地完成了其职责[^4]。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值