文档

模块激活

更新时间:

Spring 框架从 3.1.X 版本开始提供了 profile 功能,详见 Bean Definition Profiles

SOFABoot 支持模块级 profile 能力,即在各个模块启动的时候,决定在哪些 profile 中启动。profile 就是一个占位符。模块的激活主要通过配置下述文件来实现:

  • 可部署模块的 application.properties 文件。可部署模块指的是使用 spring-boot-maven-plugin 打包出 fat jar 的模块,例如 SOFABoot Web 工程中的 Web 模块。

  • 各个模块自己的 sofa-module.properties 文件。更多详情,参见 模块配置文件

模块激活

激活步骤

模块通过配置文件进行激活,操作步骤如下:

  1. 配置可部署模块的 properties 文件

    • application.properties 文件中增加如下键值对。

      • 键:com.alipay.sofa.boot.active-profiles

      • 值:为字符串,表示允许激活的 profile 列表。多个值用逗号分隔。空值表示应用所有模块均可启动。

    • 配置后,每个可以激活的模块都可以发现此字段表示的 profile 列表。

  2. 配置 SOFABoot 模块的 sofa-module.properties 文件

    • 配置下述键值对:

      • 键: Module-Profile

      • 值:为字符串,表示当前模块允许在哪些 profile 激活。多个值用逗号隔开。支持取反操作。空值表示当前 SOFABoot 模块可以在任何 profile 启动。

    • 配置示例:!dev 表示 com.alipay.sofa.boot.active-profiles 不包含 dev 时被激活。

激活示例

配置单个 profile

对上述步骤示例如下:

  1. application.properties 中增加如下配置:表示 dev 允许被激活。

    com.alipay.sofa.boot.active-profiles=dev
  2. 在 SOFABoot 模块的 sofa-module.properties 文件中增加如下配置:表示当前模块限定为在 dev 环境中被激活。

    Module-Profile=dev

配置多个 profile

对上述步骤示例如下:

  1. application.properties 中增加如下配置:表示 dev,test 允许被激活。

    com.alipay.sofa.boot.active-profiles=dev,test
  2. 在 SOFABoot 模块的 sofa-module.properties 文件中增加如下配置:表示步骤 1 配置的profile 列表中包含有 test,或 product 时,此模块将被激活。

    Module-Profile=test,product

Module-Profile 取反

对上述步骤示例如下:

  1. application.properties 中增加如下配置:表示 dev 允许被激活。

    com.alipay.sofa.boot.active-profiles=dev
  2. 在 SOFABoot 模块的 sofa-module.properties 文件中增加如下配置:表示步骤 1 配置的profile 列表中不包含 product 时,此模块将被激活。因为 profile 列表中只有 dev,因此该模块被激活。

    Module-Profile=!product

模块激活与 Spring 上下文

模块激活后,其 Spring 上下文的 spring.profiles.active 属性也会被同步配置,相应的 bean 也会被激活。示例如下:

  1. application.properties 中增加如下配置:

    com.alipay.sofa.boot.active-profiles=dev,test
  2. 在 SOFABoot 模块的 sofa-module.properties 文件中增加如下配置:表示步骤 1 配置的profile 列表中包含有 test,或 product 时,此模块将被激活。此时,该模块将被激活。

    Module-Profile=test,product
  3. 该模块被激活,则其 Spring 上下文的工作空间信息 spring.profiles.active 也被设置为了 dev,test,此时,beanId 为 devBeanIdtestBeanId 的 bean 都会被激活。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">

    <beans profile="dev">
        <bean id="devBeanId" class="com.alipay.cloudenginetest.sofaprofilesenv.DemoBean">
            <property name="name">
                <value>demoBeanDev</value>
            </property>
        </bean>
    </beans>

    <beans profile="test">
        <bean id="testBeanId" class="com.alipay.cloudenginetest.sofaprofilesenv.DemoBean">
            <property name="name">
                <value>demoBeanTest</value>
            </property>
        </bean>
    </beans>
</beans>
  • 本页导读 (0)
文档反馈