Spring boot成長系列三、Springboot配置文件詳解(application.properties/application.yaml)(爲什麼Spring boot可以做到零配置)

前言:在使用Spring boot 的時候肯定聽說過Spring boot可以做到零配置,其實創建Spring boot 確實可以做到零配置,它在內部其實幫你默認配置基礎的參數,但是它確實配置方便,所以集成的配置參數都可以在Spring boot 提供的配置文件中自己設置,除了在Spring boot提供的配置文件中配置以爲還可以做到使用java文件的方式去註冊bean,這就可以做到了Spring boot的簡化配置,不需要集成第三方功能的時候去單獨寫xml的bean。


 

1、Spring boot配置文件介紹

           spring boot之所以能簡化開發,其配置文件起到了很重要的作用,第三方集成配置都可以寫在這個配置文件裏面,這個配置文件有2個格式,application.properties和application.yaml(yml)都可以。下面來介紹2中配置文件的情況。

application.properties

                  格式:key-value
                  演示:  

server.port=8080
logging.level.root=error

application.yaml(yml)

                   格式:類似於JSON格式的形式,因爲這個格式就是JSON的超集
                   演示:  

server:
  port: 8080
logging:
  level: 
    root: info

個人比較喜歡下面這種有層級顯示的格式,後面的例子就以下面這個層級的作演示。


 2、通過不同的方式獲取配置文件的自定義值

第一種:通過@Value註解獲取配置文件裏面的值
                          1、在配置文件裏面創建你需要的數據

cx:
  name: cx
  age: 23

               2、測試

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootConfigApplicationTests {
    @Value("${cx.name}")
    private String name;
    @Value("${cx.age}")
    private String age;

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

第二種:通過@ConfigurationProperties註解獲取配置文件裏面的值生成對象

              1、在配置文件裏面創建你需要的對象數據

json: 
  str1: "雙引號直接輸出"           #雙引號字符串
  str2: '單引號可以轉義'           #單引號字符串
  boole: false                    #boolean值
  intNum: 666                     #整形
  doubleScore: 88.88              #小數
  list:                           #list集合
    - one
    - two
    - two
  arr: [1,2,2,3]                  #數組
  map: {boy: 1, girl: 2}        #map
  person:                       #集合對象
    - name: zhangsan
      age: 18
    - name: lisi
      age: 19

              2、創建實體類

@Data
public class Person {
    private String name;
    private Integer age;
}
@Data
@ConfigurationProperties(prefix = "json")
public class Cx {
    private String str1;
    private String str2;
    private Boolean boole;
    private Integer intNum;
    private Double doubleScore;
    private List<String> list;
    private Integer[] arr;
    private Map<String,Integer> map;
    private List<Person> person;
}

              3、測試

@RunWith(SpringRunner.class)
@SpringBootTest
@EnableConfigurationProperties({Cx.class})      //選擇需要註冊的類
public class SpringbootConfigApplicationTests {

    @Autowired
    private Cx cx;
    @Test
    public void testConfig(){
        System.out.println(cx);
    }
}

第三種:通過@PropertySource從指定的配置文件中讀取,這個文件只能是以properties結尾的

通過上面的幾種情況,我們可以根據開發中的不同情況進行不同的選擇。

@Value 簡單快捷,適合少數配置,少數取值,而且支持的值的類型比較少

@ ConfigurationProperties,適合當做對象取值,多數取值,支持的類型較多,基本的類型都支持

@PropertySource,適合多個配置文件,從不同的配置文件裏面加載不同的值


3、 配置文件中的佔位符

4、配置文件中配置隨機值 

配置

cx:
  age: ${random.int(10)}      #int值,後面括號是指多少以內
  score: ${random.long(100)}   #long值,後面括號是指多少以內
  secretKey: ${random.value}   #祕匙
  uuid: ${random.uuid}    #uuid

 測試

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootConfigApplicationTests {

    @Value("${cx.age}")
    private Integer age;
    @Value("${cx.score}")
    private String score;
    @Value("${cx.secretKey}")
    private String secretKey;
    @Value("${cx.uuid}")
    private String uuid;
    @Test
    public void testConfig(){
        System.out.println("祕匙:" + secretKey + ",成績:" + score + "年齡:" + age + ",uuid"+uuid);
    }
}

 


5、配置多種不同的環境開發文件

這個就像找對象一樣,是找個女朋友還是找老婆,找到誰我就娶誰(可以使用它裏面的內容)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章