Spring Boot 加載 property 順序以下:html
~/.spring-boot-devtools.properties
).@TestPropertySource
註解配置properties
:@SpringBootTest
和 測試註解.SPRING_APPLICATION_JSON
屬性ServletConfig
初始化參數ServletContext
初始化參數java:comp/env
配置的 JNDI 屬性System.getProperties()
)RandomValuePropertySource
加載 random.*
形式的屬性application-{profile}.properties
或 application-{profile}.yml
配置application-{profile}.properties
或 application-{profile}.yml
配置application.properties
或 application.yml
配置application.properties
或 application.yml
配置@PropertySource
綁定的配置SpringApplication.setDefaultProperties
指定)RandomValuePropertySource
類用於配置隨機值。java
示例:git
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
會獲取 --
參數(例如 --server.port=9000
),並將這個 property
添加到 Spring 的 Environment
中。github
若是不想加載命令行屬性,能夠經過 SpringApplication.setAddCommandLineProperties(false)
禁用。spring
SpringApplication
會自動加載如下路徑下的 application.properties
配置文件,將其中的屬性讀到 Spring 的 Environment
中。api
/config
子目錄/config
package注:bash
以上列表的配置文件會根據順序,後序的配置會覆蓋前序的配置。session
你能夠選擇 YAML(yml) 配置文件替換 properties 配置文件。oracle
若是不喜歡 application.properties
做爲配置文件名,可使用 spring.config.name
環境變量替換:app
$ java -jar myproject.jar --spring.config.name=myproject
複製代碼
可使用 spring.config.location
環境變量指定配置文件路徑:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
複製代碼
若是定義 application-{profile}.properties
形式的配置文件,將被視爲 profile
環境下的特定配置。
能夠經過 spring.profiles.active
參數來激活 profile,若是沒有激活的 profile,默認會加載 application-default.properties
中的配置。
application.properties
中的值會被 Environment
過濾,因此,能夠引用以前定義的屬性。
app.name=MyApp
app.description=${app.name} is a Spring Boot application
複製代碼
注:你可使用此技術來建立 Spring Boot 屬性變量。請參考: Section 77.4, 「Use ‘Short’ Command Line Arguments
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 中配置的屬性。
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 註解中的屬性不能經過 @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 {
}
複製代碼
Spring Boot 屬性名綁定比較鬆散。
如下屬性 key 都是等價的:
Property | Note |
---|---|
acme.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 表明的時間爲毫秒。java.time.Duration
的實現就是參照此標準)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;
}
}
複製代碼
Spring 使用 DataSize
類表明數據大小,如下場景適用:
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
複製代碼
引伸
參考