Spring Boot 2.0.0參考手冊(cè)_中英文對(duì)照_Part IV_25

文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書

25. Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded:

Spring Profiles提供了一種隔離部分應(yīng)用配置的方式,并讓這些配置只在特定的環(huán)境生效。任何帶有@Profile標(biāo)記的@Component@Configuration在加載時(shí)都會(huì)受限制:

@Configuration
@Profile("production")
public class ProductionConfiguration {

    // ...

}

In the normal Spring way, you can use a spring.profiles.active Environment property to specify which profiles are active. You can specify the property in any of the usual ways, for example you could include it in your application.properties:

以正常的Spring方式,你可以使用spring.profiles.active Environment屬性來指定激活哪一個(gè)profiles。你可以在任何常見的方式指定這個(gè)屬性,例如你可以在你的application.properties中包含它:

spring.profiles.active=dev,hsqldb

or specify on the command line using the switch --spring.profiles.active=dev,hsqldb.

或在命令行用--spring.profiles.active=dev,hsqldb轉(zhuǎn)換。

25.1 Adding active profiles

The spring.profiles.active property follows the same ordering rules as other properties, the highest PropertySource will win. This means that you can specify active profiles in application.properties then replace them using the command line switch.

spring.profiles.active屬性與其它的屬性一樣遵循同樣的排序規(guī)則,最高的PropertySource優(yōu)先。這意味著你可以在application.properties指定激活的profiles,然后用命令行轉(zhuǎn)換替代它們。

Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.includeproperty can be used to unconditionally add active profiles. The SpringApplication entry point also has a Java API for setting additional profiles (i.e. on top of those activated by the spring.profiles.active property): see the setAdditionalProfiles() method.

有時(shí)通過特定的profile屬性添加激活的profiles而不替換它們是很有用的。spring.profiles.include屬性可以用來無(wú)條件添加激活的profiles。SpringApplication入口也有設(shè)置額外的profiles的Java API(例如上面的通過spring.profiles.active屬性激活的profiles):請(qǐng)看setAdditionalProfiles()方法。

For example, when an application with following properties is run using the switch --spring.profiles.active=prod the proddb and prodmq profiles will also be activated:

例如,當(dāng)一個(gè)應(yīng)用具有以下屬性且運(yùn)行時(shí)使用--spring.profiles.active=prod轉(zhuǎn)換,proddbprodmq profiles也將被激活:

---
my.property: fromyamlfile
---
spring.profiles: prod
spring.profiles.include:
  - proddb
  - prodmq

Remember that the spring.profiles property can be defined in a YAML document to determine when this particular document is included in the configuration. See Section 70.7, “Change configuration depending on the environment” for more details.

?

記住可以在YAML文檔中定義spring.profiles來決定在配置中包含特定的文檔。更多細(xì)節(jié)請(qǐng)看70.7小節(jié),“根據(jù)環(huán)境更改配置”。

25.2 Programmatically setting profiles

You can programmatically set active profiles by calling SpringApplication.setAdditionalProfiles(…?) before your application runs. It is also possible to activate profiles using Spring’s ConfigurableEnvironment interface.

你可以在你的應(yīng)用運(yùn)行之前以編程的方式調(diào)用SpringApplication.setAdditionalProfiles(…?)來設(shè)置激活的profiles。也可以使用Spring的ConfigurableEnvironment接口來激活profiles。

25.3 Profile-specific configuration files

Profile-specific variants of both application.properties (or application.yml) and files referenced via @ConfigurationProperties are considered as files are loaded. See Section 24.4, “Profile-specific properties” for details.

application.properties(或application.yml)和通過@ConfigurationProperties引用的文件的特定profiles變種都被當(dāng)做文件進(jìn)行加載。更多細(xì)節(jié)請(qǐng)看24.4小節(jié),“Profile-specific properties”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容