springboot读取application.yaml文件数据的方法
作者:兴奋の大公猴 时间:2023-09-06 05:29:24
本文实例为大家分享了springboot读取application.yaml文件数据的具体代码,供大家参考,具体内容如下
提示:以下是本篇文章正文内容,下面案例可供参考
一、创建并编辑对应的文件
1.application.yaml
!!!这里一定要注意,datasource一定不能写成dataSource,因为会和Spring内部的产生冲突
server:
port: 8080
contry: china
user:
- name: zhangsan
age: 18
- name: lisi
age: 20
likes:
- ball
- code
- game
baseDir: c:/win10
#使用${属性名}引用数据
tempDir: ${baseDir}/temp
#创建类:用于封装下面的数据
#由spring帮我们去加载数据对象中,一定告诉spring加载这组信息
#使用时候从spring中直接获取信息使用
datasource:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/springboot_db
username: root
password: root
2.MyDataSource
package com.codejams;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//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;
@Override
public String toString() {
return "MyDataSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", 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;
}
}
二、使用步骤
1.测试代码
代码如下(示例):
package com.codejams.controller;
import com.codejams.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("books")
public class BookController {
//读取yaml文件
@Value("${contry}")
private String contry;
@Value("${user[1].name}")
private String name;
@Value("${likes[1]}")
private String like;
@Value("${tempDir}")
private String tempDir;
//使用Environment对象封装所有数据
@Autowired
private Environment env;
//查看datasource是否注入成功
@Autowired
private MyDataSource myDataSource;
@GetMapping
public String test(){
System.out.println("running...");
System.out.println(contry);
System.out.println(name);
System.out.println(like);
System.out.println(tempDir);
System.out.println("----------------------------");
System.out.println(env.getProperty("contry"));
System.out.println(env.getProperty("user[1].name"));
System.out.println("----------------------------");
System.out.println(myDataSource);
return "running..";
}
}
2.效果展示
如下(示例):
来源:https://blog.csdn.net/qq_46274901/article/details/124137423
标签:springboot,读取,application.yaml
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android编程防止进程被第三方软件杀死的方法
2021-08-01 01:52:46
Mybatis使用JSONObject接收数据库查询的方法
2023-01-17 05:10:43
![](https://img.aspxhome.com/file/2023/7/62647_0s.png)
Android编程之TextView的字符过滤功能分析
2023-05-19 20:23:36
ViewPager判断是向左划还是右划的实例
2023-12-10 04:53:18
C#实现一个控制台的点餐系统
2023-09-03 20:47:24
![](https://img.aspxhome.com/file/2023/1/113881_0s.jpg)
利用Android从0到1实现一个流布局控件
2023-01-29 06:38:15
![](https://img.aspxhome.com/file/2023/1/139001_0s.gif)
Kotlin协程概念原理与使用万字梳理
2023-03-28 21:29:32
![](https://img.aspxhome.com/file/2023/7/94287_0s.png)
Android编程实现画板功能的方法总结【附源码下载】
2023-08-05 10:45:07
![](https://img.aspxhome.com/file/2023/6/126736_0s.png)
Android编程之控件状态配置文件实例
2023-07-26 02:38:54
Android中通知Notification使用实例(振动、灯光、声音)
2021-09-28 20:00:01
![](https://img.aspxhome.com/file/2023/3/109183_0s.jpg)
Java jvm中Code Cache案例详解
2022-02-04 17:00:53
Java获取当前操作系统的信息实例代码
2023-03-07 20:00:08
Springboot Vue可配置调度任务实现示例详解
2023-11-09 03:33:19
EventBus与Spring Event区别详解(EventBus 事件机制,Spring Event事件机制)
2023-12-06 00:23:05
Java接口中尽量避免使用数组
2022-07-06 23:24:14
![](https://img.aspxhome.com/file/2023/0/69930_0s.png)
带你了解mybatis如何实现读写分离
2023-07-29 14:26:28
C# 实现简易的串口监视上位机功能附源码下载
2023-10-12 11:12:19
![](https://img.aspxhome.com/file/2023/1/106421_0s.png)
Spring Boot集成ElasticSearch实现搜索引擎的示例
2021-06-02 05:06:16
![](https://img.aspxhome.com/file/2023/3/63793_0s.png)
深入解析Android App的LayoutInflate布局
2022-01-28 19:05:26
![](https://img.aspxhome.com/file/2023/3/139423_0s.jpg)
Java实现计算器设计
2023-08-18 13:36:54
![](https://img.aspxhome.com/file/2023/7/79347_0s.jpg)