【千锋教育java教程SpringBoot2全套,springboot快速入门到项目实战视频教程】
yaml中保存的单个数据,可以使用Spring中的注解直接读取,
使用@value可以读取单个数据,属性名引用方式∶${一级属性名.二级属性名…}
server:
port: 81
#字面值
username: zhangsan
city: beijing
birthday: 2022-10-14
money: 10.9
port: 8080
# 对象
person:
name: qfedu
age: 10
# 数组类型
hobby:
- zuqiu
- lanqiu
- paiqiu
package com.dingjiaxiong.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
/**
* ClassName: HelloController
* date: 2022/10/14 15:03
*
* @author DingJiaxiong
*/
@RestController
public class HelloController {
//读取配置文件中的数据
//字符串数据
@Value("${username}")
private String username;
@Value("${city}")
private String city;
@Value("${server.port}")
private int port;
@Value("${person.name}")
private String name;
@Value("${hobby[1]}")
private String[] hobby;
@RequestMapping("/hello")
public String hello(){
System.out.println("username = " + username);
System.out.println("city = " + city);
System.out.println("port = " + port);
System.out.println("name = " + name);
System.out.println("hobby = " + Arrays.toString(hobby));
return "Hello world!!!";
}
}
运行结果

用户名【操作系统的】

这里u1s1,没黑马李老师讲得好
读取单—数据可以解决读取数据的问题,但是如果定义的数据量过大,这么一个一个书写肯定会累死人的,
SpringBoot提供了一个对象,能够把所有的数据都封装到这一个对象中,这个对象叫做Environment,使用自动装配注解可以将所有的yaml数据封装到这个对象中
package com.dingjiaxiong.controller;
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;
import java.util.Arrays;
/**
* ClassName: HelloController
* date: 2022/10/14 15:03
*
* @author DingJiaxiong
*/
@RestController
public class HelloController {
@Autowired
private Environment environment;
@RequestMapping("/hello")
public String hello(){
System.out.println(environment.getProperty("city"));
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("person.name"));
System.out.println(environment.getProperty("hobby[2]"));
System.out.println(environment.getProperty("environment"));
return "Hello world!!!";
}
}
运行结果:

数据封装到了Environment对象中,获取属性时,通过Environment的接口操作进行,具体方法时getProperties (String),参数填写属性名即可
【总结】
Java是一个面向对象的语言,很多情况下,我们会将一组数据封装成一个对象。
SpringBoot也提供了可以将一组yaml对象数据封装一个Java对象的操作
首先定义一个对象,并将该对象纳入Spring管控的范围,也就是定义成一个bean,然后使用注解@ConfigurationProperties指定该对象加载哪一组yaml中配置的信息。
这个@ConfigurationProperties必须告诉他加载的数据前缀是什么,这样当前前缀下的所有属性就封装到这个对象中。记得数据属性名要与对象的变量名一一对应,不然没法封装。
其实以后如果你要定义一组数据自己使用,就可以先写一个对象,然后定义好属性,下面到配置中根据这个格式书写即可。
举个栗子:
datasource:
driver: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db?serverTimezone=UTC
username: root
password: 200039
这是配置文件中的一个“对象”。
新建一个类
package com.dingjiaxiong.data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* ClassName: UserDataSource
* date: 2022/10/14 17:00
*
* @author DingJiaxiong
*/
@Component
@ConfigurationProperties(prefix = "datasource")
public class UserDataSource {
private String driver;
private String url;
private String username;
private String password;
public UserDataSource() {
}
public UserDataSource(String driver, String url, String username, String password) {
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserDataSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
同样在控制器中自动注入
package com.dingjiaxiong.controller;
import com.dingjiaxiong.data.UserDataSource;
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;
import java.util.Arrays;
/**
* ClassName: HelloController
* date: 2022/10/14 15:03
*
* @author DingJiaxiong
*/
@RestController
public class HelloController {
@Autowired
private UserDataSource userDataSource;
@RequestMapping("/hello")
public String hello(){
System.out.println(userDataSource.getDriver());
System.out.println(userDataSource.getUrl());
System.out.println(userDataSource.getUsername());
System.out.println(userDataSource.getPassword());
return "Hello world!!!";
}
}
访问接口测试

OK。没毛病。
当然咱们之前那个person 对象也就同样的道理了
package com.dingjiaxiong.data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* ClassName: PersonData
* date: 2022/10/14 17:12
*
* @author DingJiaxiong
*/
@Component
@ConfigurationProperties(prefix = "person")
public class PersonData {
private String name;
private int age;
public PersonData() {
}
public PersonData(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "PersonData{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试结果

OK。