1、读取application.yml中的配置属性
假设在application.yml文件中,我们定义了如下属性:
spring:
profiles:
active: dev
author:
name: blairscott # 自定义属性
---
spring:
profiles: dev
server:
port: 8081 # 系统属性
servlet:
context-path: /devpath
---
spring:
profiles: pro
server:
port: 8082
servlet:
context-path: /propath
我们期望在代码中获取到程序真正使用到的端口号,我们可以:
@RestController
public class DefController {
@Value("${server.port}") // 获取系统配置属性
private int port;
@Value("${author.name}") // 获取自定义配置属性
private String authorName;
@GetMapping("/port")
public String getPort() {
return "Current Port is " + port;
}
@GetMapping("/authorName")
public String getAuthorName() {
return "The Author Name is " + authorName;
}
}
启动程序,运行结果:
2、从属性文件中获取对象
2.1、说明
SpringBoot开发手册中提到:
文中提到,YAML文件是有缺点的,它不能以@PropertySource
注解的方式被加载,若希望以该注解的方式加载这些属性,则需要使用properties属性文件。首先,我们创建custom.properties属性文件:
2.1、获取简单对象
创建对应测试数据
student.name=张三
student.age=27
student.score=90.5
此时,我们可以通过@Value
注解的方式获取到里面的属性值。然而有的时候我们希望一次性获取到里面的所有student对应值,一次一次的取出显的很麻烦,我们更希望通过对象的方式直接获取出来,于是我们可以:
-
首先定义一个Student类
package com.blairscott.bean; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource(value = "classpath:custom.properties", encoding = "UTF-8") // 指定要读取的配置文件路径 @ConfigurationProperties("student") // 指定要读取的属性的前缀 @Data public class Student { private String name; private int age; private double score; }
-
编写Controller类测试
@RestController public class DefController { @Autowired private Student student; @GetMapping("/studentInfo") public Student getStudentInfo() { return student; } }
-
启动并查看结果
2.3、获取普通数组或集合对象
-
创建对应测试数据
country.cities[0]=beijing country.cities[1]=shanghai country.cities[2]=guangzhou
-
定义一个Country类
package com.blairscott.bean; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.List; @Component @PropertySource("classpath:custom.properties") @ConfigurationProperties("country") @Data public class Country { private List<String> cities; }
-
编写Controller测试类
@RestController public class DefController { @Autowired private Country country; @GetMapping("/countryInfo") public Country getCountryInfo() { return country; } }
-
启动并查看结果
2.4、获取对象数组或集合
-
创建对应测试数据
company.departs[0].name=研发部 company.departs[0].level=10 company.departs[0].nums=9 company.departs[1].name=教学部 company.departs[1].level=11 company.departs[1].nums=13 company.departs[2].name=市场部 company.departs[2].level=12 company.departs[2].nums=30
-
定义Company类和Department类
@Data public class Department { private String name; private int level; private int nums; }
@Component @PropertySource("classpath:custom.properties") @ConfigurationProperties("company") @Data public class Company { private List<Department> departs; }
-
编写Controller测试类
@RestController public class DefController { @Autowired private Company company; @GetMapping("/company") public Company getCompany() { return company; } }
-
启动并查看结果