文章作者: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.include
property 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)換,proddb
和prodmq
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”。