1 什么是組態檔
- 指
main-resources-Application.properties或Application.yml檔案,這兩個都是全域組態檔 - 若不存在則new file新建
- 用于修改自動配置的默認值
2 YAML語言
-
是一種用于組態檔的標記語言
-
以前的組態檔一般用xml,而YAML以資料為中心(沒有那么多開閉標簽)
-
語法
-
key:value表示一個鍵值對,冒號和value之間必須有空格
-
以空格控制縮進來控制層級關系(那么只要左對齊就是同級)
-
區分大小寫
-
value的書寫
-
普通變數:Key: value
- 雙引號,不會轉義特殊字符,\n表示回車
- 單引號,會轉義,\n是普通字串
-
物件:用縮進寫屬性
-
縮進寫法

-
行內寫法

-
-
陣列:用-表示一個元素
-
縮進寫法

-
行內寫法
? Pets: [cat,dog,pig]
-
-
-
PS:寫的時候可以注意下面的提示

3 組態檔應用實體:設定埠值
-
new java class:
bean.Person -
new Java class :
Dog -
定義Person類和Dog類【
Alt+insert鍵,可以一鍵匯入get/set/toString方法】 -
在pom里面匯入組態檔處理器的依賴項(不然會not found錯誤)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> -
寫好person的組態檔application.yml
-
把組態檔的值系結到java類中
-
person類前面加入:
@ConfiguationProperties(perfix="person")——告訴springboot,本類所有的屬性都是由組態檔確定
——perfix,確定將組態檔中哪個屬性進行一一映射
-
在person類前面加上@Component
——因為還要是組件內的容器,才能使用容器提供的ConfiguationProperties
-
-
在測驗單元里面,注入person,看測驗容器收到的person是不是組態檔設定的
這里本來應該是
@Autowired,但是出現錯誤,修改成@Autowired(required=false)解決,

-
運行
contextLoads()函式,控制臺列印出person的資訊如下:可見,與組態檔的設定相同,

附代碼:
Person.java
package com.atguigu.springboot02config.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
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;
}
}
Dog.java
package com.atguigu.springboot02config.bean;
public class Dog {
private String Name;
private Integer age;
@Override
public String toString() {
return "Dog{" +
"Name='" + Name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
application.yml
person:
lastName: zhangsan
age: 90
boss: false
birth: 2020/10/1
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
name: wawa
age: 2
SpringBoot02ConfigApplicationTests.java
package com.atguigu.springboot02config;
import com.atguigu.springboot02config.bean.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.awt.*;
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringBoot02ConfigApplicationTests<person> {
@Autowired(required = false)
Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
4 properties組態檔的寫法
- 普通語法:
在application.properties里寫入:
person.last-name=xxx
person.age=18
person.birth=2017/10/10
person.boss=false
person.maps.k1=v1
person.k2=14
person.lists=a,b,c
person.dog.name=doggie
person.dog.age=1
設定好后,同樣運行contextLoads()函式,也可以得到正確的person
但是會有中文亂碼問題,解決方法:
在idea-setting-搜索file encoding-下方選擇UTF-8-勾選Transparent …選項
- 亂數的寫法:
person.last-name=kris${random.uuid}
person.age=${random.int}
- 為變數設定默認值:
person.dog.name=${person.hello:hello}_dog
${person.hello:hello}_dog:如果沒有設定過person.hello這個東西,就會原樣列印:{person.hello}_dog
${person.hello:hello}_dog:沒有設定過person.hello這個東西,person.hello就會變成默認值hello,總體列印成hello_dog
5 @Value
之前的yml和properties都是用了@ConfiguationProperties注解,而除此之外還可以使用@Value注解
如:
@Component
public class Person{
@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
}
得到age=22
二者區別如下:
| @ConfiguationProperties | @Value | |
|---|---|---|
| 注入 | 批量設定變數值 | 一個個指定 |
| 松散系結 | √ | × |
| spEL | ×(不可計算) | √ |
| JSR303資料校驗 | √ | × |
| 復雜型別(如map) | √ | √ |
- 松散系結:就是支持下面幾種寫法意義一樣

- spEL(就是能不能計算)

- JSR303資料校驗:指可以加上一個@Validated表示要校驗某個變數的格式,如@Email表示某個變數必須是郵箱格式,格式錯誤就會報錯
6 加載指定的組態檔
剛剛都是加載全域組態檔application.properties,現在想加載別的檔案來當組態檔
方法1:
- 在resources檔案夾-新建檔案
person.properties - 用
@PropertySource(value={classpath:person.properties}):加載指定組態檔
更推薦的方法(全注解方法):
- 添加配置類:在com下,new java class-
config.MyAppConfig @Configuration表明當前類是配置類,相當于組態檔@Bean將方法的回傳值添加到容器中

這是使用配置類給容器添加組件(將方法的回傳值添加到容器中),在容器中標識組件的默認id是方法的方法名(helloService)
測驗容器中有沒有“helloService”這個組件

7 profile 多組態檔
在生產、開發、測驗不同環境下,可能需要不同的組態檔來實作不同的配置
而主程式默認使用的是application.properties的配置
實作方法1:
-
在resources檔案夾新建
application-pro.properties(pro環境模擬),application-dev.properties(dev環境模擬),application-test.properties(test環境模擬)(注意:格式得是
application-xxx.properties或者application-xxx.yml) -
在全域配置
application.properties頭上寫spring.profiles.active=dev,指定哪個環境被激活
實作方法2(檔案塊方式):
- 在
application.yml,用---表示分割,上面叫document1,下面是document2,同理分割多個 - 指定
spring.profiles.active=dev,如以下代碼,運行的就是dev環境的埠,否則默認8081
server:
port: 8081
spring:
profiles:
active: dev
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: pro
8 組態檔加載位置
8.1 內部組態檔
springboot會識別以下四個位置的組態檔,而且是按優先級高到低依次往下

- file指專案的根目錄
- classpath指的是類路徑,這里是main-resources
識別的時候會覆寫+互補配置
- 覆寫:高優先級和低優先級都有,只采用高優先級的(但是也會加載低的)
- 互補配置:指如果在高優先級沒有配置的話會采用低優先級的配置
專案打包好以后啟動時,可以使用命令列引數的形式來指定組態檔的新位置,這時候指定的組態檔和默認加載的組態檔共同起作用形成互補配置;
——用于運維,已經打包好了不需要重新打包
8.2 外部組態檔
配置還可以寫在這17個位置,同理也有“優先級順序+覆寫+互補”

其中較為重要的有:
-
命令列形式直接修改配置:
多個引數之間用空格隔開,如–server=… --path=…

-
來自java:comp/env的JNDI屬性
-
Java系統屬性(System.getProperties())
-
作業系統環境變數
-
RandomValuePropertySource配置的random.*屬性值
【由jar包外向jar包內進行尋找; 優先加載帶pro?le,再來加載不帶pro?le】
-
jar包外部的application-{pro?le}.properties或application.yml(帶spring.pro?le)組態檔
-
jar包內部的application-{pro?le}.properties或application.yml(帶spring.pro?le)組態檔
-
jar包外部的application.properties或application.yml(不帶spring.pro?le)組態檔
-
jar包內部的application.properties或application.yml(不帶spring.pro?le)組態檔
-
@Con?guration注解類上的@PropertySource
-
通過SpringApplication.setDefaultProperties指定的默認屬性
所有支持的配置加載來源;
9 @Conditional組件
有些組件需要設定成在特定條件下生效,
@Conditional這個組件為配置的生效設定了條件,如下表:

那怎么更方便的知道哪些配置生效?
-
在
application.properties寫上debug=true -
控制臺列印資訊中,Positive matches 表示生效的,Negetive matches表示沒有生效的
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/160251.html
標籤:其他
下一篇:基礎篇:深入決議JAVA注解機制
