SpringBoot的配置
- 組態檔
- YAML語法
- 基本語法
- 值的寫法
- 字面量:普通的值(數字,字串,布爾)
- 物件、Map(屬性和值)(鍵值對)
- 陣列(List、Set)
- 組態檔值注入
- 代碼演示
- 匯入組態檔處理器,使撰寫配置有提示
- properties組態檔在idea中默認utf-8可能會亂碼
- @Value獲取值和@ConfigurationProperties獲取值比較
- 組態檔注入值資料校驗
- @PropertySource&@ImportResource&@Bean
- @PropertySource:加載指定的組態檔
- @ImportResource:匯入Spring的組態檔,讓組態檔里面的內容生效,使用@Bean給容器中添加組件
- 組態檔占位符
- 亂數
- 占位符獲取之前配置的值,如果沒有可以是用:指定默認值
- Profile
- 多Profile檔案
- yml支持多檔案塊方式
- 激活指定profile
- ?在組態檔中指定 spring.profiles.active=dev
- 命令列
- 虛擬機引數
- 組態檔加載位置
- 優先級由高到底,高優先級的配置會覆寫低優先級的配置
- SpringBoot會從這四個位置全部加載主組態檔,并且互補配置
- 我們還可以通過spring.config.location來改變默認的組態檔位置
- 外部配置加載順序
組態檔
SpringBoot使用一個全域的組態檔,組態檔名是固定的(
application.properties或application.yml),
組態檔的作用:修改SpringBoot自動配置的默認值,SpringBoot在底層都給我們自動配置好了,
以前的組態檔;大多都使用的是 xxxx.xml檔案,
.yml是YAML語言的檔案,以資料為中心,比json、xml等更適合作為組態檔,
配置例子:
YAML:
server:
port: 8081
XML:
<server>
<port>8081</port>
</server>
YAML語法
基本語法
- 使用縮進表示層級關系
- 縮進時不允許使用Tab鍵,只允許使用空格
- 縮進的空格數目不重要,只要相同層級的元素左側對齊即可
- 大小寫敏感
application.yml:
server:
port: 8081
path: /hello
值的寫法
YAML 支持的三種資料結構:
- 字面量:單個的、不可再分的值
- 物件:鍵值對的集合
- 陣列:一組按次序排列的值
字面量:普通的值(數字,字串,布爾)
k: v:字面直接來寫
字串默認不用加上單引號或者雙引號
“”:雙引號:不會轉義字串里面的特殊字符
特殊字符會作為本身想表示的意思
name: "zhangsan \n lisi"輸出:zhangsan 換行 lisi
單引號:會轉義特殊字符,特殊字符最終只是一個普通的字串資料
name: 'zhangsan \n lisi'輸出:zhangsan \n lisi
物件、Map(屬性和值)(鍵值對)
k: v 在下一行來寫物件的屬性和值的關系,注意縮進和空格,
物件還是k: v的方式
friends:
lastName: Keafmd
age: 20
行內寫法:
friends: {lastName: Keafmd,age: 18}
陣列(List、Set)
用 - 值表示陣列中的一個元素,
pets:
‐ cat
‐ dog
‐ pig
行內寫法:
pets: [cat,dog,pig]
組態檔值注入
代碼演示
Person:
package com.keafmd.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Keafmd
*
* @ClassName: Person
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-23 12:47
*/
/**
* 將組態檔中配置的每一個屬性的值,映射到這個組件中
* @ConfigurationProperties :告訴SpringBoot將本類中的所有屬性和組態檔相關的配置進行系結
* prefix = "person" :組態檔中哪個屬性進行一一映射
*
* 只有這個組件時容器中的組件嗎,才能使用容器提供的功能
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
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{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birthday=" + birthday +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
Dog:
package com.keafmd.springboot.bean;
/**
* Keafmd
*
* @ClassName: Dog
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-23 12:50
*/
public class Dog {
private String name;
private Integer age;
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;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
application.yml:
person:
lastName: Keafmd
age: 18
boss: false
birthday: 2020/02/02
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
- Keafmd
- xiaolan
dog:
name: 二狗
age: 2
測驗代碼:
package com.keafmd.springboot;
import com.keafmd.springboot.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
/**
* SpringBoot單元測驗
* 可以再測驗期間很方便的類似編碼一樣進行自動注入等容器
*/
@SpringBootTest
class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
運行結果:
Person{lastName='Keafmd', age=18, boss=false, birthday=Sun Feb 02 00:00:00 CST 2020, maps={k1=v1, k2=v2}, lists=[lisi, zhaoliu, Keafmd, xiaolan], dog=Dog{name='二狗', age=2}}
匯入組態檔處理器,使撰寫配置有提示
注意:我們可以在pom.xml中匯入組態檔處理器,以后撰寫配置就有提示了,
<!--匯入組態檔處理器,組態檔進行系結就會有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
properties組態檔在idea中默認utf-8可能會亂碼
把application.yml的person注釋掉,在application.properties檔案中進行撰寫,
application.properties:
# 配置person的值
person.last-name=張三
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=二狗
person.dog.age=3
我們再次運行測驗代碼會發現,會出現中文亂碼的情況,
解決辦法:

設定好再次運行測驗代碼
運行結果:
Person{lastName='張三', age=18, boss=false, birthday=Sun Feb 02 00:00:00 CST 2020, maps={k1=v1, k2=v2}, lists=[a, b, c], dog=Dog{name='二狗', age=3}}
@Value獲取值和@ConfigurationProperties獲取值比較
| - | @ConfigurationProperties | @Value |
|---|---|---|
| 功能 | 批量注入組態檔中的屬性 | 一個個指定 |
| 松散系結(松散語法) | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303資料校驗 | 支持 | 不支持 |
| 復雜型別封裝 | 支持 | 不支持 |
組態檔yml還是properties他們都能獲取到值
如果說,我們只是在某個業務邏輯中需要獲取一下組態檔中的某項值,使用@Value
如果說,我們專門撰寫了一個javaBean來和組態檔進行映射,我們就直接使用@ConfigurationProperties
application.properties:
# 配置person的值
person.last-name=張三
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=二狗
person.dog.age=3
Person:(部分代碼,get和set以及toString不展示了)
package com.keafmd.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Keafmd
*
* @ClassName: Person
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-23 12:47
*/
/**
* 將組態檔中配置的每一個屬性的值,映射到這個組件中
* @ConfigurationProperties :告訴SpringBoot將本類中的所有屬性和組態檔相關的配置進行系結
* prefix = "person" :組態檔中哪個屬性進行一一映射
*
* 只有這個組件時容器中的組件嗎,才能使用容器提供的功能
*/
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
/**
* <bean class = "Person">
* <property name = "lastName" value = "字面量/${key}從環境變數、組態檔中獲取值/#{SpEL}"></property>
* </bean>
*/
@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
運行測驗代碼的運行結果:
Person{lastName='張三', age=22, boss=true, birthday=null, maps=null, lists=null, dog=null}
屬性名匹配規則(Relaxed binding)
– person.firstName:使用標準方式
– person.first-name:大寫用-
– person.first_name:大寫用_
– PERSON_FIRST_NAME:
? 推薦系統屬性使用這種寫法
組態檔注入值資料校驗
@Value 不支持資料校驗,
需要先在pom.xml引入校驗的依賴:
<!--引入校驗-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Person:(部分代碼,get和set以及toString不展示了)
package com.keafmd.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Keafmd
*
* @ClassName: Person
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-23 12:47
*/
/**
* 將組態檔中配置的每一個屬性的值,映射到這個組件中
* @ConfigurationProperties :告訴SpringBoot將本類中的所有屬性和組態檔相關的配置進行系結
* prefix = "person" :組態檔中哪個屬性進行一一映射
*
* 只有這個組件時容器中的組件嗎,才能使用容器提供的功能
*/
@Component
@ConfigurationProperties(prefix = "person")
@Validated //校驗
public class Person {
/**
* <bean class = "Person">
* <property name = "lastName" value = "字面量/${key}從環境變數、組態檔中獲取值/#{SpEL}"></property>
* </bean>
*/
//@Value("${person.last-name}")
//lastName必須是郵箱格式
@Email
private String lastName;
//@Value("#{11*2}")
private Integer age;
//@Value("true")
private Boolean boss;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
運行測驗類的結果:

@PropertySource&@ImportResource&@Bean
@PropertySource:加載指定的組態檔
person.properties:
person.last-name=Keafmd-person
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=二狗
person.dog.age=3
我們想加載這個組態檔的內容,就必須用 @PropertySource指定組態檔
package com.keafmd.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Keafmd
*
* @ClassName: Person
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-23 12:47
*/
/**
* 將組態檔中配置的每一個屬性的值,映射到這個組件中
* @ConfigurationProperties :告訴SpringBoot將本類中的所有屬性和組態檔相關的配置進行系結
* prefix = "person" :組態檔中哪個屬性進行一一映射
*
* 只有這個組件時容器中的組件嗎,才能使用容器提供的功能
*/
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated //校驗
public class Person {
/**
* <bean class = "Person">
* <property name = "lastName" value = "字面量/${key}從環境變數、組態檔中獲取值/#{SpEL}"></property>
* </bean>
*/
//@Value("${person.last-name}")
//lastName必須是郵箱格式
//@Email
private String lastName;
//@Value("#{11*2}")
private Integer age;
//@Value("true")
private Boolean boss;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
運行測驗類的測驗結果:
Person{lastName='Keafmd-person', age=18, boss=false, birthday=Wed Feb 02 00:00:00 CST 2022, maps={k1=v1, k2=v2}, lists=[a, b, c], dog=Dog{name='二狗', age=3}}
注意:需要把
application.properties和application.yml里的person注釋掉,
@ImportResource:匯入Spring的組態檔,讓組態檔里面的內容生效,使用@Bean給容器中添加組件
Spring Boot里面沒有Spring的組態檔,我們自己撰寫的組態檔,也不能自動識別,
如果想讓Spring的組態檔生效,加載進來,需要把@ImportResource標注在一個配置類上,
@ImportResource(locations = {"classpath:beans.xml"})
匯入Spring的組態檔讓其生效
創建一個Spring的組態檔beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.keafmd.springboot.service.HelloService">
</bean>
</beans>
創建一個helloService:
package com.keafmd.springboot.service;
/**
* Keafmd
*
* @ClassName: HelloService
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-23 15:59
*/
public class HelloService {
}
先測驗下容器中有沒有helloService:
package com.keafmd.springboot;
import com.keafmd.springboot.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
/**
* SpringBoot單元測驗
* 可以再測驗期間很方便的類似編碼一樣進行自動注入等容器
*/
@SpringBootTest
class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
public void testHelloService(){
Boolean b = ioc.containsBean("helloService");
System.out.println(b);
}
@Test
public void contextLoads() {
System.out.println(person);
}
}
運行testHelloService的測驗結果:
false
此時說明組態檔并沒有生效,
我們把@ImportResource標注在主配置類上,
SpringBoot02ConfigApplication :
package com.keafmd.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
}
}
再次運行測驗方法:
true

但是在實際開發中我們會采用一種更合適的方法,SpringBoot推薦給容器中添加組件的方式是推薦使用全注解的方式,接下來把
@ImportResource(locations = {"classpath:beans.xml"})注釋掉,創建一個配置類MyAppConfig ,
1、創建配置類,使用 @Configuration ------>Spring組態檔
2、使用 @Bean給容器中添加組件
==MyAppConfig: ==
package com.keafmd.springboot.config;
import com.keafmd.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Keafmd
*
* @ClassName: MyAppConfig
* @Description: 配置類
* @author: 牛哄哄的柯南
* @date: 2021-02-23 16:06
*/
/**
* @Configuration: 指明當前類是配置類,替代之前的Spring組態檔
* 在組態檔中用<bean></bean>標簽添加組件
*/
@Configuration
public class MyAppConfig {
//將方法的回傳值添加到容器中,容器中這個組件默認的id就是方法名
@Bean
public HelloService helloService(){
System.out.println("配置類@Bean給容器添加組件了,,,");
return new HelloService();
}
}
再次運行測驗方法:

組態檔占位符
亂數
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
占位符獲取之前配置的值,如果沒有可以是用:指定默認值
# 配置person的值
person.last-name=Keafmd${random.uuid}
#person.last-name=張三
#person.age=${random.int}
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
#person.dog.name=${person.last-name}的二狗
person.dog.name=${person.hello:hello}的二狗
#person.dog.name=二狗
person.dog.age=3
Profile
多Profile檔案
我們在主組態檔撰寫的時候,檔案名可以是 application-{profile}.properties/yml
默認使用application.properties的配置

yml支持多檔案塊方式
server:
port: 8081
spring:
profiles:
active: dev #指定屬于哪個環境
---
server:
port: 8083
spring:
config:
activate:
on-profile: dev
---
server:
port: 8084
spring:
profiles: prod #不推薦的寫法
---

激活指定profile
?在組態檔中指定 spring.profiles.active=dev
application.properties:
server.port=8081
spring.profiles.active=dev
命令列
1、可以在這里配置


2、也可以直接在測驗的時候,配置傳入命令列引數
打包后運行jar包時,輸入下面的命令:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
虛擬機引數
-Dspring.profiles.active=dev

組態檔加載位置
springboot 啟動會掃描以下位置的application.properties或者application.yml檔案作為Spring boot的默認組態檔
–file:./config/
–file:./
–classpath:/config/
–classpath:/
優先級由高到底,高優先級的配置會覆寫低優先級的配置

SpringBoot會從這四個位置全部加載主組態檔,并且互補配置
例子:
我們創建個HelloController:
package com.keafmd.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Keafmd
*
* @ClassName: HelloController
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-23 17:07
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
這是上面優先級第四的組態檔內容:
server.port=8081
# 配置專案的訪問路徑
server.servlet.context-path=/boot02
這是上面優先級第一的組態檔內容:
server.port=8084
最后我們啟動程式,
運行結果:


我們還可以通過spring.config.location來改變默認的組態檔位置
我們先在F盤放一個組態檔,在這個組態檔把埠改為8085:
server.port=8085

測驗一下:

說明這樣是不起作用的,
我們先將專案打包,生成jar包,

專案打包好以后,我們可以使用命令列引數的形式,啟動專案的時候來指定組態檔的新位置;指定組態檔和默認加載的這些組態檔共同起作用形成互補配置,
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.additional-location=F:/application.properties
打開便捷的控制臺:

輸入:java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.additional-location=F:/application.properties,然后回車:

我們就可以發現,是啟動的8085埠,證明啟用的F盤的組態檔,

外部配置加載順序
SpringBoot也可以從以下位置加載配置,優先級從高到低,高優先級的配置覆寫低優先級的配置,所有的配置會形成互補配置
1. 命令列引數
所有的配置都可以在命令列上進行指定
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.servlet.context-path=/abc
多個配置用空格分開, --配置項=值
運行結果:


2. 來自java:comp/env的JNDI屬性
3. Java系統屬性(System.getProperties())
4. 作業系統環境變數
5. RandomValuePropertySource配置的random. * 屬性值
由jar包外向jar包內進行尋找,優先加載帶profile

6. jar包外部的application-{profile}.properties或application.yml(帶spring.profile)組態檔
7. jar包內部的application-{profile}.properties或application.yml(帶spring.profile)組態檔
再來加載不帶profile
8. jar包外部的application.properties或application.yml(不帶spring.profile)組態檔
9. jar包內部的application.properties或application.yml(不帶spring.profile)組態檔
10. @Configuration注解類上的@PropertySource
11. 通過SpringApplication.setDefaultProperties指定的默認屬性
所有支持的配置加載來源:參考官方檔案

以上就是SpringBoot的配置【組態檔、加載順序、配置原理】(超詳細)的全部內容,
看完如果對你有幫助,感謝點贊支持!
如果你是電腦端的話,看到右下角的 “一鍵三連” 了嗎,沒錯點它[哈哈]

加油!
共同努力!
Keafmd
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/263458.html
標籤:python
