天天看點

spring-boot學習二:Spring boot配置相關内容1.配置檔案

1.配置檔案

1.1簡單介紹

spring boot預設會使用一個全局的配置檔案,這個配置檔案可以是兩種格式的檔案,一種是.properties,一種是.yaml格式的檔案;并且配置檔案的名字是固定的,要麼是application.properties,要麼是application.yaml;

spring-boot學習二:Spring boot配置相關内容1.配置檔案

配置檔案的作用:修改 Spring Boot配置的預設值,例如端口号等資訊;

1.2YAML文法簡介

1.2.1簡介

YAML:(YAML Ain't  Markup Lanuage),這個叫法有兩層含義:

YAML A Markup Lanuage:YAML就是一個标記語言;

YAML isn't Markup Lanuage:YAMA不是一個标記語言;

YAML以資料為中心,比json.xml更适合做配置檔案;

比如我們想改變spring boot 啟動的端口資訊,我們在application.properties檔案中的文法如下所示:

server.port=8971      

但是在application.yaml檔案中的文法如下所示:

server:
  port: 8089      

當然如果同樣的工作我們在XML檔案中進行配置,文法如下:

<server>
<port>8089</port>
</server>      

 1.2.2具體文法

【1】k:(空格)v:表示一對鍵值對(空格必須有),以空格的縮進來控制層級關系;隻要是左對齊的一列資料,都是同一層級的; 

server:
  port: 8089
  error:
    path: /hello      

其中server為第一層級,port和error屬于同一個層級,path單獨屬于一個層級;

還需要注意的是,yaml檔案中的屬性和值大小寫敏感;

【2】值的寫法

即在yaml檔案中都可以寫入哪些值,我們常見的值類型如下:

第一類:字面量,普通的值(數字、字元串、布爾)

             k: v:字面直接來寫,尤其對于字元串類型不需要加入雙引号,如果加上雙引号有特殊含義,會将字元串中的特殊字元進行專義輸出,例如輸出"ab \n c",輸出:  

ab
c      

如果加的是單引号,那麼特殊字元将會被原樣輸出,例如:'ab \n c' 輸出就是:

ab \n c      

第二類:對象、map(屬性和值)(鍵值對);

   k: v:在下一行來寫對象的屬性和值的關系,注意縮進;例如我們現在有個類User,裡面有屬性name和age寫法如下:

user:
    name: haige
    age: 20      

  上述的寫法還等價于下面這種行内寫法:

User{name: haige,age: 20}      

第三類:集合數組(List、Set);

   用-值表示數組中的一個元素,例如下面有個集合pets,裡面有集合原素:pig、dog、cat寫法如下:

pets:
    -pig
    -dog
    -cat      

述的寫法還等價于下面這種行内寫法:

pets: [pig,cat,dog]      

 1.3從YAML配置檔案注入屬性值

既然YAML配置檔案這麼友善,那我們需要學會從YAML檔案中讀取屬性。下面通過一個執行個體進行示範;

具體步驟如下所示:

【1】第一步:聲明一個類Person,裡面有屬性name、age、map集合、list集合,對象等屬性;

package com.hai.bao.springboot01;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author :haibao.wang
 * @date :Created in 2019/8/29 22:20
 * @description:person類
 * @modified By:
 * @version: $
 */
public class Person {
    private String name;
    private int age;
    private boolean boss;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isBoss() {
        return boss;
    }

    public void setBoss(boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}      

在Person類裡面有一個Dog類的屬性,Dog類如下:

package com.hai.bao.springboot01;

/**
 * @author :haibao.wang
 * @date :Created in 2019/8/29 22:23
 * @description:建立一個小狗類
 * @modified By:
 * @version: $
 */
public class Dog {
    private String dogName;
    private int dogAge;

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public int getDogAge() {
        return dogAge;
    }

    public void setDogAge(int dogAge) {
        this.dogAge = dogAge;
    }
}      

【2】在application.yaml檔案中配置Person類中相關屬性的值;

server:
  port: 8089
person:
  name: 海哥
  age: 18
  boss: true
  birth: 1990/05/27
  maps:
    key01: 化學
    key02: 實體
  lists:
    - 跑步
    - 遊泳
  dog:
    dogName: 旺财
    dogAge: 3      
spring-boot學習二:Spring boot配置相關内容1.配置檔案

 【3】此時在Person類中有提示資訊:

spring-boot學習二:Spring boot配置相關内容1.配置檔案

 此時需要在pom.xml檔案中添加配置檔案處理器,如下依賴;

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>      

 【4】給Person類加上@ConfigurationProperties(prefix = "Person")注解。告訴Spring  boot将本類中的所有屬性和配置檔案中的配置資訊一一綁定;

spring-boot學習二:Spring boot配置相關内容1.配置檔案

 【5】給Person類加上容器的注解,確定元件在容器中,才能夠使用@ConfigurationProperties注解;

spring-boot學習二:Spring boot配置相關内容1.配置檔案

 【6】啟動Spring boot服務;

【7】寫一個Spring  boot的單元測試類,來驗證下功能:

package com.hai.bao;

import com.hai.bao.springboot01.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BaoApplicationTests {
    @Autowired
    Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}      

代碼運作結果如下:

spring-boot學習二:Spring boot配置相關内容1.配置檔案

1.4在.properties檔案中去配置Person類的相關屬性;

#配置Person類中相關的屬性值
person.age=10
person.birth=1990/05/01
person.boss=true
person.maps.key01=數學
person.maps.key02=化學
person.lists=瑜伽,遊泳
person.dog.dogName=旺财
person.dog.dogAge=2      
spring-boot學習二:Spring boot配置相關内容1.配置檔案

 啟動後執行單元測試,測試結果如下:

spring-boot學習二:Spring boot配置相關内容1.配置檔案

 有亂碼,因為IDEA原生的編碼格式為utf-8,解決亂碼問題的方法如下:

File-settings-File Encodings

 @configurationproperties注解和@Value注解的具體差別如下:

  • @configurationproperties注解可以批量注入屬性,但是@Value隻能一個個的注入屬性
  • @configurationproperties支援松散綁定,但是@Value不支援松散綁定;
  • @configurationproperties支援JSR303資料校驗,@Value不支援;
  • @configurationproperties不支援SpEl,但是@Value支援;
  • @configurationproperties支援複雜類型資料,例如map集合,但是@Value不支援;

是以:如果說我們隻是在業務邏輯中擷取一個業務的值。我們就直接使用@Value注解即可;

    如果我們專門編寫一個javaBean來和配置檔案進行一一映射,這個時候就使用@Configurationproperties注解;

轉載于:https://www.cnblogs.com/haibaowang/p/11426980.html