欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot yaml語法與數(shù)據(jù)讀取操作詳解

 更新時間:2022年07月18日 10:01:57   作者:不會壓彎的小飛俠  
YAML 是 “YAML Ain’t Markup Language”(YAML 不是一種標(biāo)記語言)的遞歸縮寫。在開發(fā)的這種語言時,YAML 的意思其實(shí)是:“Yet Another Markup Language”(仍是一種標(biāo)記語言),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下

yaml

YAML是一種數(shù)據(jù)序列化格式。

yaml擴(kuò)展名

  • .yaml
  • .yml(主流)

yaml語法規(guī)則

  • 大小寫敏感
  • 屬性層次關(guān)系使用多行描述,每行結(jié)尾使用冒號結(jié)束
  • 使用縮進(jìn)表示層級關(guān)系,同層左側(cè)對齊,只允許使用空格(不允許使用Tab鍵)
  • 屬性值前面添加空格(屬性名與屬性值之間使用冒號+空格作為分隔)
  • #表示注釋

字面值表示方式:

boolean: true
float: 3.14
int: 15
#表示空
null: ~
string: xiaofeixia
date: 2022-7-9
#日期與時間用T連接
datetime: 2022-7-9T12:00:30+02:00

數(shù)組表示方式:

likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

對象數(shù)組格式:

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

對象數(shù)組縮略格式:

user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]

讀取yaml數(shù)據(jù)

使用@Value讀取單個數(shù)據(jù),屬性名引用方式:${一級屬性名.二級屬性名}

編寫yaml文件

server:
  port: 81
country: china
province: henan
city: zhengzhou
area: shangqiu

party: true
birthday: 2022-11-11

user8:
  name: xiaofeixia
  age: 22
user1:
  name: xiaofeixia
  age: 22

a:
  B:
    C:
      d:
        e: abc

likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]

讀取單一數(shù)據(jù)

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
    //讀取yaml數(shù)據(jù)中的單一數(shù)據(jù)
    @Value("${country}")
    public String country1;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");
        System.out.println("country=="+country1);  //country==china      
        return "springboot is running...";
    }
}

讀取二級數(shù)據(jù)

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
    @Value("${user8.name}")
    public String username;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("username=="+username);         //username==xiaofeixia            
        return "springboot is running...";
    }
}

讀取數(shù)組數(shù)據(jù)

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
 @Value("${likes[0]}")
    public String likes1;
     @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("likes1=="+likes1);  //likes1==music      
        return "springboot is running...";
    }
}

讀取服務(wù)器端口號

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
   @Value("${server.port}")
    public String port;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("port=="+port);  //port==81   
        return "springboot is running...";
    }
}

讀取對象屬性

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
    @Value("${user2[0].age}")
    public String age2;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
         System.out.println("age2=="+age2);  //age2==22         
        return "springboot is running...";
    }
}

封裝全部數(shù)據(jù)到Environment對象

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
    @Autowired
    private Environment env;
    @GetMapping
    public String ById(){        
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("user8.name"));        
        return "springboot is running...";
    }
}

讀取yaml引用類型屬性數(shù)據(jù)

application.yml

server:
  port: 81
#創(chuàng)建類用于封裝下面的數(shù)據(jù)
#由spring去加載數(shù)據(jù)到對象中,一定要告訴spring加載這組信息
#使用的時候直接從spring中獲取信息
datasource:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/springboot
  username: root
  password: root

MyDataSource

自定義對象封裝指定數(shù)據(jù)

1.定義數(shù)據(jù)模型封裝yaml文件中對應(yīng)的數(shù)據(jù)

package com.jkj;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//2.定義spring的管控Bean
@Component
//3.指定加載數(shù)據(jù)
@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 + '\'' +
                '}';
    }
}

讀取數(shù)據(jù)

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
    @Autowired
    private MyDataSource myDataSource;
    @GetMapping
    public String ById(){     
        System.out.println(myDataSource);
        //MyDataSource{driver='com.mysql.jdbc.Driver', url='jdbc:mysql://localhost/springboot', username='root', password='root'}
        return "springboot is running...";
    }
}

變量的引用

application.yml

server:
  port: 81
baseDir: E:\window
tempDir: ${baseDir}\temp

讀取數(shù)據(jù)

package com.jkj.controller;
import com.jkj.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("/yamlBooks")
public class BookController {
     @Value("${tempDir}")
     public String temp;   
    @GetMapping
    public String ById(){     
         System.out.println("temp=="+temp);  //temp==E:\window\temp
        return "springboot is running...";
    }
}

context-path

只寫:

server:
  port: 81

控制臺輸出:path為空

加上context-path后:

server:
  port: 81
  servlet:
    context-path: /test

控制臺輸出頁面:

注意:在瀏覽器輸入:http://localhost:81/test/yamlBooks 進(jìn)行測試。

@Autowired報錯解決方案

  • file
  • setting
  • 搜索Spring Core
  • 如下圖所示將Severity修改為Warning

到此這篇關(guān)于SpringBoot yaml語法與數(shù)據(jù)讀取操作詳解的文章就介紹到這了,更多相關(guān)SpringBoot yaml語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 跨域問題的處理方式

    Java 跨域問題的處理方式

    這篇文章主要介紹了Java 跨域問題的處理方式,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • maven profile自動切換環(huán)境參數(shù)的2種方法詳解

    maven profile自動切換環(huán)境參數(shù)的2種方法詳解

    這篇文章主要給大家介紹了關(guān)于maven profile自動切換環(huán)境參數(shù)的2種方法,文中通過示例代碼將這兩種方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • 全面了解Java中Native關(guān)鍵字的作用

    全面了解Java中Native關(guān)鍵字的作用

    下面小編就為大家?guī)硪黄媪私釰ava中Native關(guān)鍵字的作用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • 深入了解Java中的static關(guān)鍵字

    深入了解Java中的static關(guān)鍵字

    這篇文章主要為大家詳細(xì)介紹了Java中的static關(guān)鍵字的用法的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-11-11
  • Spring的懶加載機(jī)制原理和配置詳解

    Spring的懶加載機(jī)制原理和配置詳解

    這篇文章主要介紹了Spring的懶加載機(jī)制原理和配置詳解,Spring提供了懶加載機(jī)制,所謂的懶加載機(jī)制就是可以規(guī)定指定的bean不在啟動時立即創(chuàng)建,而是在后續(xù)第一次用到時才創(chuàng)建,從而減輕在啟動過程中對時間和內(nèi)存的消耗,需要的朋友可以參考下
    2023-10-10
  • springboot與mybatis整合實(shí)例詳解

    springboot與mybatis整合實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了springboot與mybatis整合實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Thymeleaf中th:each及th:if使用方法解析

    Thymeleaf中th:each及th:if使用方法解析

    這篇文章主要介紹了Thymeleaf中th:each及th:if使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • mybatis plus in方法使用說明

    mybatis plus in方法使用說明

    這篇文章主要介紹了mybatis plus in方法使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • JavaSE實(shí)現(xiàn)猜拳游戲

    JavaSE實(shí)現(xiàn)猜拳游戲

    這篇文章主要為大家詳細(xì)介紹了JavaSE實(shí)現(xiàn)猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 詳解Elasticsearch如何實(shí)現(xiàn)簡單的腳本排序

    詳解Elasticsearch如何實(shí)現(xiàn)簡單的腳本排序

    Elasticsearch?是位于?Elastic?Stack?核心的分布式搜索和分析引擎,可以為所有類型的數(shù)據(jù)提供近乎實(shí)時的搜索和分析。本文主要介紹了Elasticsearch如何實(shí)現(xiàn)簡單的腳本排序,感興趣的可以了解一下
    2023-01-01

最新評論