欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringBoot教程之屬性加載詳解-創(chuàng)新互聯(lián)

目錄

  • 加載 property 順序

    公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出海豐免費做網(wǎng)站回饋大家。
  • 隨機屬性

  • 命令行屬性

  • Application 屬性文件

  • Profile 特定屬性

  • 屬性中的占位符

  • YAML 屬性

  • 訪問屬性

  • 多 profile 配置

  • YAML 的缺點

  • 屬性前綴

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

  • 屬性轉(zhuǎn)換

  • 時間單位轉(zhuǎn)換

  • 數(shù)據(jù)大小轉(zhuǎn)換

  • 校驗屬性

加載 property 順序

Spring Boot 加載 property 順序如下:

  1. Devtools 全局配置?(當(dāng) 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 包內(nèi)的?application-{profile}.properties?或?application-{profile}.yml?配置

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

  15. jar 包內(nèi)的?application.properties?或?application.yml?配置

  16. @PropertySource?綁定的配置

  17. 默認(rèn)屬性 (通過?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]}

命令行屬性

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

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

Application 屬性文件

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

  1. 當(dāng)前目錄的?/config?子目錄

  2. 當(dāng)前目錄

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

  4. classpath 根路徑

注:
以上列表的配置文件會根據(jù)順序,后序的配置會覆蓋前序的配置。
你可以選擇?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,默認(rèn)會加載?application-default.properties?中的配置。

屬性中的占位符

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

app.name=MyApp
app.description=${app.name}?is?a?Spring?Boot?application
注:你可以使用此技術(shù)來創(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 配置轉(zhuǎn)化為 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)?{?...?}

	}
}

相當(dāng)于支持配置以下屬性:

  • 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大寫字母

屬性轉(zhuǎn)換

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

時間單位轉(zhuǎn)換

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

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

  • ISO-8601 標(biāo)準(zhǔn)格式(?java.time.Duration?的實現(xiàn)就是參照此標(biāo)準(zhǔ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ù)據(jù)大小轉(zhuǎn)換

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

  • long 值(默認(rèn)視為 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,需要自己領(lǐng)取。

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站名稱:SpringBoot教程之屬性加載詳解-創(chuàng)新互聯(lián)
文章路徑:http://chinadenli.net/article44/diiche.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣云服務(wù)器營銷型網(wǎng)站建設(shè)虛擬主機Google外貿(mào)建站

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)