springBoot中获取配置文件中的数据

29 阅读1分钟

一、先来看配置文件的两种格式yml和properties 1.yml

#email
email:
  user: 123465
  password: 123456
#数组的形式:
hobbies:
    - 吃饭
    - 睡觉
    - 喝酒
    - 唱歌

2.properties

email.username=123456
email.password=132456

二、获取数据,使用注解将数据封装到实体类的成员变量上 1.使用@Value("${key}")

@Value("${email.user}")

2.使用@ConfigurationProperties(prefix = "前缀") 在使用之前保证prefix与配置文件中的数据相同层级的共同前缀,例如上述所说email就是前缀,所以这里的prefix="email"

@ConfigurationProperties(prefix = "email")