Spring Boot 2.0.0参考手册_中英文对照_Part IV_25

标签: spring
852人阅读 评论(0) 收藏 举报

博客:noahsnail.com  |  CSDN  |  简书

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提供了一种隔离部分应用配置的方式,并让这些配置只在特定的环境生效。任何带有@Profile标记的@Component@Configuration在加载时都会受限制:

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属性来指定激活哪一个profiles。你可以在任何常见的方式指定这个属性,例如你可以在你的application.properties中包含它:


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


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.


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.

有时通过特定的profile属性添加激活的profiles而不替换它们是很有用的。spring.profiles.include属性可以用来无条件添加激活的profiles。SpringApplication入口也有设置额外的profiles的Java API(例如上面的通过spring.profiles.active属性激活的profiles):请看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:

例如,当一个应用具有以下属性且运行时使用--spring.profiles.active=prod转换,proddbprodmq profiles也将被激活:

my.property: fromyamlfile
spring.profiles: prod
  - 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.



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.


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变种都被当做文件进行加载。更多细节请看24.4小节,“Profile-specific properties”。


* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    • 访问:321469次
    • 积分:6382
    • 等级:
    • 排名:第3807名
    • 原创:287篇
    • 转载:0篇
    • 译文:81篇
    • 评论:58条
    硕士毕业,目前在外企从事Deep Learning相关工作。
    微信打赏: 微信
    支付宝打赏: 支付宝