springboot讀取application.yaml文件數據的方法
更新時間:2022年07月29日 11:58:06 作者:興奮の大公猴
這篇文章主要為大家詳細介紹了springboot讀取application.yaml文件數據的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了springboot讀取application.yaml文件數據的具體代碼,供大家參考,具體內容如下
提示:以下是本篇文章正文內容,下面案例可供參考
一、創(chuàng)建并編輯對應的文件
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
#創(chuàng)建類:用于封裝下面的數據
#由spring幫我們去加載數據對象中,一定告訴spring加載這組信息
#使用時候從spring中直接獲取信息使用
datasource:
? driver: com.mysql.jdbc.Driver
? url: jdbc:mysql://localhost/springboot_db
? username: root
? password: root2.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.效果展示
如下(示例):

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程
這篇文章主要介紹了Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
SpringCloud Gateway動態(tài)轉發(fā)后端服務實現過程講解
這篇文章主要介紹了SpringCloud Gateway動態(tài)轉發(fā)后端服務實現過程,簡單的路由轉發(fā)可以通過SpringCloudGateway的配置文件實現,在一些業(yè)務場景種,會需要動態(tài)替換路由配置中的后端服務地址,單純靠配置文件無法滿足這種需求2023-03-03

