一篇文章帶你掌握主流辦公框架——SpringBoot( 四 )

  • 大小寫敏感
  • 屬性層級關系
  • 使用縮進表示層級關系,同層級左側對齊,只允許使用空格(不能使用tab)
  • 屬性值前面添加空格(屬性名與屬性值之間使用冒號+空格作為分隔)
  • # 表示注釋
  • 使用 - 來表示數據開始符號(數組)
YAML語法使用規范示例:
server:port: 82logging:level:root: infolikes:- music- game- PEYAML的數據讀取方法:
首先我們先給出我們在yml文件中所列出的屬性:
lesson: SpringBootserver:port: 80enterprise:name: itcastage: 16tel: 4006184000subject:- Java- 前端- 大數據下面我們來介紹yaml數據讀取的三種方法:
  1. ${屬性名},${屬性名.屬性名},${屬性名.屬性名[數組下標]}
package com.itheima.controller;import com.itheima.domain.Enterprise;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.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/books")public class BookController {//使用@Value讀取單一屬性數據@Value("${lesson}")private String lesson;@Value("${server.port}")private Integer port;@Value("${enterprise.subject[0]}")private String subject_00;@GetMapping("/{id}")public String getById(@PathVariable Integer id){System.out.println(lesson);System.out.println(port);System.out.println(subject_00);return "hello , spring boot!";}}
  1. Environment對象匹配方法
package com.itheima.controller;import com.itheima.domain.Enterprise;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.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/books")public class BookController {//使用Environment封裝全配置數據(自動裝配封裝Environment,里面會包含yaml中所有屬性和屬性值)@Autowiredprivate Environment environment;@GetMapping("/{id}")public String getById(@PathVariable Integer id){// 我們采用environment的getProperty方法,根據屬性名,獲得屬性值System.out.println(environment.getProperty("lesson"));System.out.println(environment.getProperty("server.port"));System.out.println(environment.getProperty("enterprise.age"));System.out.println(environment.getProperty("enterprise.subject[1]"));return "hello , spring boot!";}}
  1. 自定義對象封裝指定數據
// 自定義對象Enterprise實現類(屬于Domain)package com.itheima.domain;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import java.util.Arrays;//封裝yaml對象格式數據必須先聲明當前實體類受Spring管控@Component//使用@ConfigurationProperties注解定義當前實體類讀取配置屬性信息 , 通過prefix屬性設置讀取哪個數據@ConfigurationProperties(prefix = "enterprise")public class Enterprise {private String name;private Integer age;private String tel;private String[] subject;@Overridepublic String toString() {return "Enterprise{" +"name='" + name + '\'' +", age=" + age +", tel='" + tel + '\'' +", subject=" + Arrays.toString(subject) +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;}public String[] getSubject() {return subject;}public void setSubject(String[] subject) {this.subject = subject;}}// 服務層Controllerpackage com.itheima.controller;import com.itheima.domain.Enterprise;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.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/books")public class BookController {// 自動裝配實現類即可@Autowiredprivate Enterprise enterprise;@GetMapping("/{id}")public String getById(@PathVariable Integer id){System.out.println(enterprise);return "hello , spring boot!";}}<!--實現自定義對象封裝時會產生警告,我們需要添加以下依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>

推薦閱讀