使用 SOFABoot 的 profile 功能,需要在 application.properties 文件增加 com.alipay.sofa.boot.active-profiles 字段,该字段的值为逗号分隔的字符串,表示允许激活的 profile 列表,指定该字段后,SOFABoot 会为每个可以激活的模块指定此字段表示的 profile 列表。

SOFABoot 模块的 sofa-module.properties 文件支持 Module-Profile 字段,该字段的值为逗号分隔的字符串,表示当前模块允许在哪些 profile 激活。Module-Profile 支持取反操作, !dev 表示 com.alipay.sofa.boot.active-profiles 不包含 dev 时被激活。

当应用未指定 com.alipay.sofa.boot.active-profiles 参数时,表示应用所有模块均可启动。SOFABoot 模块未指定 Module-Profile 时,表示当前 SOFABoot 模块可以在任何 profile 启动。

使用例子

application.properties 中增加配置如下:

在每个需要限定为 dev profile 被激活模块的 sofa-module.properties 文件中增加如下配置:

application.properties 中增加配置如下:

该配置表示激活 profile 为 dev 或者 test 的模块。

在 SOFABoot 模块的 sofa-module.properties 文件中增加如下配置:

  1. Module-Profile=test,product

application.properties 中增加配置如下:

该配置表示激活 profile 为 dev 的模块。

在 SOFABoot 模块的 sofa-module.properties 文件中增加如下配置:

  1. Module-Profile=!product

该配置表示当 com.alipay.sofa.boot.active-profiles 不包含 product 时激活模块,由于当前指定的 com.alipay.sofa.boot.active-profiles 为 dev ,此模块将被激活。

该配置表示激活 profile 为 dev 或者 test 的模块,当一个模块满足上面的激活条件时,这个模块就会被启动,同时 Spring 上下文的环境信息 spring.profiles.active 也被设置为了 dev,test ,这样如下的配置 beanId 为 devBeanId 和 testBeanId 的bean都会被激活。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jee="http://www.springframework.org/schema/jee"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context/spring-context.xsd"
  8. default-autowire="byName">
  9. <beans profile="dev">
  10. <bean id="devBeanId" class="com.alipay.cloudenginetest.sofaprofilesenv.DemoBean">
  11. <property name="name">
  12. <value>demoBeanDev</value>
  13. </bean>
  14. <beans profile="test">
  15. <bean id="testBeanId" class="com.alipay.cloudenginetest.sofaprofilesenv.DemoBean">
  16. <property name="name">
  17. <value>demoBeanTest</value>
  18. </property>
  19. </bean>
  20. </beans>
  21. </beans>