properties/yml配置文件中的属性值获取

一 通过@ConfigurationProperties获取值(默认从全局配置文件中取值)
1.创建Bean
@ConfigurationProperties----告诉SpringBoot将本类中的属性与配置文件中相关配置(prefix=“xxx”)进行绑定

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Date birthday;
    private Boolean boy;
    
    private List<Object> list;
    private Map<String,Object> maps;

    private Animal animal;
    ...
    get/set方法
    ...
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", boy=" + boy +
                ", list=" + list +
                ", maps=" + maps +
                ", animal=" + animal +
                '}';
    }
package com.mine.bean;

public class Animal {
    private String name;
    private Integer 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;
    }

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

2 导入配置文件处理器、测试启动器

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3
application.yml

person:
  name: LiDa
  age: 20
  birthday: 1995/02/23
  boy: true
  list:
    - lier
    - lisan
  maps: {k1: str,k2: 12}
  animal:
    name: 大狗
    age: 4

application.properties

#配置Person属性值

person.name=李大
person.age=20
person.birthday=1995/02/21
person.boy=true
person.list=a,b,c
person.maps.k1=value1
person.maps.k2=12
person.animal.name=大狗
person.animal.age=3

4 SpringBoot单元测试

//SpringBoot可以在运行期间很方便的类似编码一样进行自动注入容器等功能

@RunWith(SpringRunner.class) //使用Spring的驱动器运行
@SpringBootTest
public class ReadPropertiesTest {

    @Autowired
    private Person person;

    @Test
    public void propertiesTest()
    {
        System.out.println(person);
        //Person{name='LiDa', age=20, birthday=Thu Feb 23 00:00:00 CST 1995, boy=true, list=[lier, lisan], maps={k1=str, k2=12}, animal=Animal{name=大狗, age=4}}
    }
}

二 通过@value获取配置文件中的值

@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
    @Value("${person.name}")
    private String name;
    @Value("${person.age}") //或者使用SPEL直接赋值---#{12*2}
    private Integer age;
    private Date birthday;
    private Boolean boy;


    private List<Object> list;
    private Map<String,Object> maps;

    private Animal animal;

三 @Value和@ConfigurationProperties获取值比较

@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个个指定
SPEL不支持支持
复杂数据类型封装支持支持
JSR303数据校验支持不支持
松散绑定支持不支持
取值全局配置文件(application.properties/yml)全局配置文件

当只需要获取配置文件中某个值时,使用@Value
当对JavaBean的属性与配置文件进行映射,使用@ConfigurationProperties

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值