SpringBoot读取propriety/yml/yaml配置中的内容
3种方法
方法1 @value
@RestController
public class HelloController {
/*@Value("${XXXX}") 这个是@Value的固定格式,XXXX是配置在propriety/yml/yaml里面的内容
拿yml里面的文件举例子:
name1: 李四
person:
name: chen
age: 18
这样就有下面的 @Value("${name1}")和 @Value("${person.name}")和 @Value("${person.age}")
*/
@Value("${name1}")
private String name;
@Value("${person.name}")
private String name2;
@Value("${person.age}")
private int age;
@Value("${address[0]}")
private String address;
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
@RequestMapping("hello2")
public String Hello2() {
System.out.println(" --> " + name);
System.out.println(" --> " + name2);
System.out.println(" --> " + age);
System.out.println(" --> " + address);
System.out.println(" --> " + msg1);
System.out.println(" --> " + msg2);
return "hello_SpringBoot";
}
方法2:Environment
/*
1-用@Autowired
private Environment environment;
2-调用方法:environment.getProperty("XXXX")
//这个XXXX是propriety/yml/yaml里面的配置名,如果不懂请参考@Value里面的代码备注
*/
@RestController
public class HelloController {
@Autowired
private Environment environment;
@RequestMapping("hello3")
public String Hello3() {
System.out.println(environment.getProperty("name1"));
System.out.println(environment.getProperty("address[0]"));
return "hello_SpringBoot3";
}
方法3:@ConfigurationProperties
1-propriety/yml/yaml配置信息
#只是拿这个person来举例子
person:
name: chen
age: 18
address:
- fuzhou
- jiangxi
2-我们现在需要把这个配置文件里面的person用@ConfigurationProperties方法读取出来,首先需要写一个Person的类
/*
1-先把propriety/yml/yaml里面需要配置的东西写成一个`类`
2- private String name;和 private int age;里面的name、age是propriety/yml/yaml里面名字一样的
eg:propriety/yml/yaml里面是这样:
person:
name: chen
age: 18
address:
- fuzhou
- jiangxi
*/
@Component
@ConfigurationProperties(prefix = "person")
//@ConfigurationProperties(prefix = "person") 这个需要定义:定义一下你需要导入的是哪个配置,我这个和注释一样导入person,一般和这个类名定义一致
public class Person {
private String name;
private int age;
private String[] address;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + Arrays.toString(address) +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
}
3-把他打印出来(因为1-类有get、set方法,又因为2-@ConfigurationProperties(prefix = "person"))把配置文件内容赋值给Person类了,所以直接打印出来就是配置文件的内容了)
@RestController
public class HelloController {
@Autowired
private Person person;
@RequestMapping("hello4")
public String Hello4() {
System.out.println(person);//把类的信息打印出来
String[] address = person.getAddress();
for (String add:address) {
System.out.println(add);
}
return "hello_SpringBoot4";
}
}
4-出现的问题:在自定义的Person类中
解决:
1-点击Open Documentation
2-找到这个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3-加到pom.xml里面
4-效果:以后写配置文件会有提示