文章目录
- yaml文件中变量引用
- 遇到问题
- 改为
- 尝试读取
- 测试成功
- yaml读取支持转义字符
- 测试成功
- 小结
- yaml读取全部属性数据
- 使用Environment对象封装信息
- 测试成功
- 注意
- 读取yaml引用类型属性数据
- 编写实体类
- 读取
- 读取测试(null)
- 成功测试
- servlet: context -path
- 小结
yaml文件中变量引用
遇到问题
相同文件路径名,数据较多时,改的时候不能一起改,所以我们直接引用这个路径
改为
尝试读取
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
@Value("${tempDir}")
private String temp;
@GetMapping
public String getById(){
System.out.println("spring boot is running");
System.out.println("port====>" + temp);
return "spring boot is running";
}
}
测试成功
yaml读取支持转义字符
要使用双引号引起来
测试成功
小结
yaml读取全部属性数据
使用Environment对象封装信息
使用@Autowired自动装配数据到Environment对象中
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
//使用自动装配将所有的数据封装到一个对象Environment中
@Autowired
private Environment env;
@GetMapping
public String getById(){
System.out.println("spring boot is running");
System.out.println(env.getProperty("server.port"));
System.out.println(env.getProperty("user.name"));
return "spring boot is running";
}
}
测试成功
注意
Environment导入的是SpringBoot包下的接口
读取yaml引用类型属性数据
datasource:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/text
username: root
password: 12345
编写实体类
package com.taotao;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/5/1 11:36
*/
@SuppressWarnings({"all"})
//1.定义数据模型封装yaml文件中对应的数据
//2.定义为spring管控的bean
@Component
//3.指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
private String driver;
private String url;
private String username;
private String 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 "MyDataSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
读取
package com.taotao.controller;
import com.taotao.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
//使用自动装配将所有的数据封装到一个对象Environment中
@Autowired
private Environment env;
private MyDataSource myDataSource;
@GetMapping
public String getById(){
System.out.println("spring boot is running");
System.out.println(env.getProperty("server.port"));
System.out.println(env.getProperty("user.name"));
System.out.println("------------------------");
System.out.println(myDataSource);
return "spring boot is running";
}
}
读取测试(null)
没有对对象进行自动装配
成功测试
servlet: context -path
访问前缀
server:
port: 80
servlet:
context-path: /taotao
小结