SpringBoot教程之屬性加載詳解

目錄

  • 加載 property 順序

    茅箭網站制作公司哪家好,找創(chuàng)新互聯(lián)!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、響應式網站設計等網站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創(chuàng)新互聯(lián)。

  • 隨機屬性

  • 命令行屬性

  • Application 屬性文件

  • Profile 特定屬性

  • 屬性中的占位符

  • YAML 屬性

  • 訪問屬性

  • 多 profile 配置

  • YAML 的缺點

  • 屬性前綴

  • 屬性松散綁定規(guī)則

  • 屬性轉換

  • 時間單位轉換

  • 數(shù)據大小轉換

  • 校驗屬性

加載 property 順序

Spring Boot 加載 property 順序如下:

  1. Devtools 全局配置?(當 devtools 被激活?\~/.spring-boot-devtools.properties).

  2. ="https://docs.spring.io/spring/docs/5.1.3.RELEASE/javadoc-api/org/springframework/test/context/TestPropertySource.html">測試環(huán)境中的 @TestPropertySource 注解配置

  3. 測試環(huán)境中的屬性?properties@SpringBootTest?和?測試注解.

  4. 命令行參數(shù)

  5. SPRING_APPLICATION_JSON?屬性

  6. ServletConfig?初始化參數(shù)

  7. ServletContext?初始化參數(shù)

  8. JNDI attributes from 通過?java:comp/env?配置的 JNDI 屬性

  9. Java 系統(tǒng)屬性 (System.getProperties())

  10. 操作系統(tǒng)環(huán)境比那里

  11. RandomValuePropertySource?加載?random.*?形式的屬性

  12. jar 包外的?application-{profile}.properties?或?application-{profile}.yml?配置

  13. jar 包內的?application-{profile}.properties?或?application-{profile}.yml?配置

  14. jar 包外的?application.properties?或?application.yml?配置

  15. jar 包內的?application.properties?或?application.yml?配置

  16. @PropertySource?綁定的配置

  17. 默認屬性 (通過?SpringApplication.setDefaultProperties?指定)

隨機屬性

RandomValuePropertySource?類用于配置隨機值。

示例:

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

命令行屬性

默認情況下,?SpringApplication?會獲取?--?參數(shù)(例如?--server.port=9000?),并將這個?property?添加到 Spring 的?Environment?中。

如果不想加載命令行屬性,可以通過?SpringApplication.setAddCommandLineProperties(false)禁用。

Application 屬性文件

SpringApplication?會自動加載以下路徑下的?application.properties?配置文件,將其中的屬性讀到 Spring 的?Environment?中。

  1. 當前目錄的?/config?子目錄

  2. 當前目錄

  3. classpath 路徑下的?/config?package

  4. classpath 根路徑

注:
以上列表的配置文件會根據順序,后序的配置會覆蓋前序的配置。
你可以選擇?YAML(yml)?配置文件替換 properties 配置文件。

如果不喜歡?application.properties?作為配置文件名,可以使用?spring.config.name?環(huán)境變量替換:

$?java?-jar?myproject.jar?--spring.config.name=myproject

可以使用?spring.config.location?環(huán)境變量指定配置文件路徑:

$?java?-jar?myproject.jar?--spring.config.location=classpath:/default.properties,classpath:/override.properties

Profile 特定屬性

如果定義?application-{profile}.properties?形式的配置文件,將被視為?profile?環(huán)境下的特定配置。

可以通過?spring.profiles.active?參數(shù)來激活 profile,如果沒有激活的 profile,默認會加載?application-default.properties?中的配置。

屬性中的占位符

application.properties?中的值會被?Environment?過濾,所以,可以引用之前定義的屬性。

app.name=MyApp
app.description=${app.name}?is?a?Spring?Boot?application
注:你可以使用此技術來創(chuàng)建 Spring Boot 屬性變量。請參考:?Section 77.4, “Use ‘Short’ Command Line Arguments

YAML 屬性

Spring Framework provides two convenient classes that can be used to load YAML documents. The?YamlPropertiesFactoryBean?loads YAML as?Properties?and the?YamlMapFactoryBean?loads YAML as a?Map.

Spring 框架有兩個類支持加載 YAML 文件。

  • YamlPropertiesFactoryBean?將 YAML 文件的配置加載為?Properties?。

  • YamlMapFactoryBean?將 YAML 文件的配置加載為?Map?。

示例 1

environments:
	dev:
		url:?http://dev.example.com
		name:?Developer?Setup
	prod:
		url:?http://another.example.com
		name:?My?Cool?App

等價于:

environments.dev.url=http://dev.example.com
environments.dev.name=Developer?Setup
environments.prod.url=http://another.example.com
environments.prod.name=My?Cool?App

YAML 支持列表形式,等價于 property 中的?[index]?:

my:
servers:
	-?dev.example.com
	-?another.example.com

等價于

my.servers[0]=dev.example.com
my.servers[1]=another.example.com

訪問屬性

YamlPropertySourceLoader?類會將 YAML 配置轉化為 Spring?Environment?類中的?PropertySource?。然后,你可以如同 properties 文件中的屬性一樣,使用?@Value?注解來訪問 YAML 中配置的屬性。

多 profile 配置

server:
??address:?192.168.1.100
---
spring:
??profiles:?development
server:
??address:?127.0.0.1
---
spring:
??profiles:?production?&?eu-central
server:
??address:?192.168.1.120

YAML 的缺點

注:YAML 注解中的屬性不能通過?@PropertySource?注解來訪問。所以,如果你的項目中使用了一些自定義屬性文件,建議不要用 YAML。

屬性前綴

package?com.example;

import?java.net.InetAddress;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;

import?org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="acme")
public?class?AcmeProperties?{

	private?boolean?enabled;

	private?InetAddress?remoteAddress;

	private?final?Security?security?=?new?Security();

	public?boolean?isEnabled()?{?...?}

	public?void?setEnabled(boolean?enabled)?{?...?}

	public?InetAddress?getRemoteAddress()?{?...?}

	public?void?setRemoteAddress(InetAddress?remoteAddress)?{?...?}

	public?Security?getSecurity()?{?...?}

	public?static?class?Security?{

		private?String?username;

		private?String?password;

		private?List<String>?roles?=?new?ArrayList<>(Collections.singleton("USER"));

		public?String?getUsername()?{?...?}

		public?void?setUsername(String?username)?{?...?}

		public?String?getPassword()?{?...?}

		public?void?setPassword(String?password)?{?...?}

		public?List<String>?getRoles()?{?...?}

		public?void?setRoles(List<String>?roles)?{?...?}

	}
}

相當于支持配置以下屬性:

  • acme.enabled

  • acme.remote-address

  • acme.security.username

  • acme.security.password

  • acme.security.roles

然后,你需要使用?@EnableConfigurationProperties?注解將屬性類注入配置類中。

@Configuration
@EnableConfigurationProperties(AcmeProperties.class)
public?class?MyConfiguration?{
}

屬性松散綁定規(guī)則

Spring Boot 屬性名綁定比較松散。

以下屬性 key 都是等價的:

PropertyNoteacme.my-project.person.first-name-分隔acme.myProject.person.firstName駝峰命名acme.my_project.person.first_name_分隔ACME_MYPROJECT_PERSON_FIRSTNAME大寫字母

屬性轉換

如果需要類型轉換,你可以提供一個?ConversionService?bean (一個名叫?conversionService的 bean) 或自定義屬性配置 (一個?CustomEditorConfigurer?bean) 或自定義的?Converters?(被?@ConfigurationPropertiesBinding?注解修飾的 bena)。

時間單位轉換

Spring 使用?java.time.Duration?類代表時間大小,以下場景適用:

  • 除非指定?@DurationUnit?,否則一個 long 代表的時間為毫秒。

  • ISO-8601 標準格式(?java.time.Duration?的實現(xiàn)就是參照此標準)

  • 你也可以使用以下支持的單位:

    • ns?- 納秒

    • us?- 微秒

    • ms?- 毫秒

    • s?- 秒

    • m?- 分

    • h?- 時

    • d?- 天

示例:

@ConfigurationProperties("app.system")
public?class?AppSystemProperties?{

	@DurationUnit(ChronoUnit.SECONDS)
	private?Duration?sessionTimeout?=?Duration.ofSeconds(30);

	private?Duration?readTimeout?=?Duration.ofMillis(1000);

	public?Duration?getSessionTimeout()?{
		return?this.sessionTimeout;
	}

	public?void?setSessionTimeout(Duration?sessionTimeout)?{
		this.sessionTimeout?=?sessionTimeout;
	}

	public?Duration?getReadTimeout()?{
		return?this.readTimeout;
	}

	public?void?setReadTimeout(Duration?readTimeout)?{
		this.readTimeout?=?readTimeout;
	}

}

數(shù)據大小轉換

Spring 使用?DataSize?類代表數(shù)據大小,以下場景適用:

  • long 值(默認視為 byte)

  • 你也可以使用以下支持的單位:

    • B

    • KB

    • MB

    • GB

    • TB

示例:

@ConfigurationProperties("app.io")
public?class?AppIoProperties?{

	@DataSizeUnit(DataUnit.MEGABYTES)
	private?DataSize?bufferSize?=?DataSize.ofMegabytes(2);

	private?DataSize?sizeThreshold?=?DataSize.ofBytes(512);

	public?DataSize?getBufferSize()?{
		return?this.bufferSize;
	}

	public?void?setBufferSize(DataSize?bufferSize)?{
		this.bufferSize?=?bufferSize;
	}

	public?DataSize?getSizeThreshold()?{
		return?this.sizeThreshold;
	}

	public?void?setSizeThreshold(DataSize?sizeThreshold)?{
		this.sizeThreshold?=?sizeThreshold;
	}

}

校驗屬性

@ConfigurationProperties(prefix="acme")
@Validated
public?class?AcmeProperties?{

	@NotNull
	private?InetAddress?remoteAddress;

	@Valid
	private?final?Security?security?=?new?Security();

	//?...?getters?and?setters

	public?static?class?Security?{

		@NotEmpty
		public?String?username;

		//?...?getters?and?setters

	}

}

你也可以自定義一個名為?configurationPropertiesValidator?的校驗器 Bean。獲取這個?@Bean?的方法必須聲明為?static。

使用方法:

mvn?clean?package
cd?target
java?-jar?sbe-core-property.jar

本人免費整理了Java高級資料,涵蓋了Java、redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并發(fā)分布式等教程,一共30G,需要自己領取。

傳送門:https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q

名稱欄目:SpringBoot教程之屬性加載詳解
本文路徑:http://bm7419.com/article48/jdcehp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設、App開發(fā)、外貿網站建設、網站改版、云服務器

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網站優(yōu)化