在上一篇中,我们已经知道了SpringBoot的配置文件如何设置。并且知道不单单可以配置 SpringBoot 框架定义的参数,还可以设置我们自定义的参数。
那么本章节,我们就来看看如何获取配置的参数。
下面我们来逐步演示这三种方式。
package com.lijw.springbootinit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping
public String hello(){
return "hello SpringBoot....";
}
}
使用浏览器访问:http://localhost:8082/hello
server:
port: 8082
name: libai
person:
name: ${name}
age: 35
address:
- beijing
- shenzhen
msg1: 'hello \n world' # 单引忽略转义字符
msg2: "hello \n world" # 双引识别转义字符
package com.lijw.springbootinit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
// #获取普通配置
@Value("${name}")
private String name;
// 获取对象属性
@Value("${person.name}")
private String name2;
// 获取数组
@Value("${address[0]}")
private String address1;
//获取纯量
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
@RequestMapping
public String hello(){
System.out.println("name: " + name);
System.out.println("person.name: " + name2);
System.out.println("address1: " + address1);
System.out.println("msg1: " + msg1);
System.out.println("msg2: " + msg2);
return "hello SpringBoot....";
}
}
请求Crontroller查看打印的信息如下:
package com.lijw.springbootinit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
// #获取普通配置
@Value("${name}")
private String name;
// 获取对象属性
@Value("${person.name}")
private String name2;
// 获取数组
@Value("${address[0]}")
private String address1;
//获取纯量
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
@Autowired
private Environment env;
@RequestMapping
public String hello(){
System.out.println("name: " + name);
System.out.println("person.name: " + name2);
System.out.println("address1: " + address1);
System.out.println("msg1: " + msg1);
System.out.println("msg2: " + msg2);
System.out.println("--------------------");
System.out.println("env name: " + env.getProperty("name"));
System.out.println("env person.name: " + env.getProperty("person.name"));
System.out.println("env address1: " + env.getProperty("address[1]"));
System.out.println("env msg1: " + env.getProperty("msg1"));
System.out.println("env msg2: " + env.getProperty("msg2"));
return "hello SpringBoot....";
}
}
注意:prefix一定要写,如果不写,则会根据属性读取配置文件中存在的key
package com.lijw.springbootinit;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component // 设置为组件
@ConfigurationProperties(prefix = "person") // 配置读取数据,前缀为 person
public class Person {
private String name;
private Integer age;
private String[] address;
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;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + Arrays.toString(address) +
'}';
}
}
package com.lijw.springbootinit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private Person person;
@RequestMapping
public String hello(){
System.out.println(person);
return "hello SpringBoot....";
}
}
好了,这样我们就已经通过定义 Person 类,以及配置 @ConfigurationProperties(prefix = "person")
读取到配置文件里面的数据了。
但是我们可以发现编辑器上出现这个提示信息:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
添加依赖如下:
此时,我们编写配置文件,发现没有提示了?
这需要我们删除其他配置文件,避免冲突,如下:
保留一个配置文件即可。