Module activation

Updated at:

The Spring framework has provided the profile feature since version 3.1.X. For more information, see Bean Definition Profiles.

SOFABoot supports the module-level profile capability, that is, when each module is started, it determines which profile to start in. A profile is a placeholder. The module is activated by configuring the following files:

  • The application.properties file for the deployable module. Deployable modules are modules that use spring-boot-maven-plugin to package fat jar, such as the web module in the SOFABoot web project.

  • Each module has its own sofa-module.properties file. For more information, see Module configuration files.

Module activation

Activation steps

The module is activated by using the configuration file. The procedure is as follows:

  1. Configure the properties file of a deployable module

    • Add the following key-value pairs to the application.properties file.

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

      • Value: a string that indicates the list of profiles that are allowed to be activated. Separate multiple values with commas. A null value indicates that all modules of the application can be started.

    • Once configured, each module that can be activated can discover the list of profiles represented by this field.

  2. Configure the sofa-module.properties file of the SOFABoot module

    • Configure the following key-value pairs:

      • Key: Module-Profile.

      • Value: a string indicating in which profiles the current module is allowed to be activated. Separate multiple values with commas. Inverse operations are supported. A null value indicates that the current SOFABoot module can be started in any profile.

    • Configuration example: ! dev representation com.alipay.sofa.boot.active-profiles is activated when dev is not included.

Activation example

Configure a single profile

An example of the preceding steps is as follows:

  1. Add the following configuration to the application.properties: This indicates that dev can be activated.

    com.alipay.sofa.boot.active-profiles=dev
  2. Add the following configuration to the sofa-module.properties file of the SOFABoot module: indicating that the current module is limited to be activated in the dev environment.

    Module-Profile=dev

Configure multiple profiles

An example of the preceding steps is as follows:

  1. Add the following configuration to the application.properties: indicating that dev and test can be activated.

    com.alipay.sofa.boot.active-profiles=dev,test
  2. Add the following configuration to the sofa-module.properties file of the SOFABoot module: This indicates that the module will be activated when the profile list configured in Step 1 contains test or product.

    Module-Profile=test,product

Module-Profile negation (Condition exclusion)

An example of the preceding steps is as follows:

  1. Add the following configuration to the application.properties: This indicates that dev can be activated.

    com.alipay.sofa.boot.active-profiles=dev
  2. Add the following configuration to the sofa-module.properties file of the SOFABoot module: This indicates that this module will be activated when the profile list configured in Step 1 does not contain product. Because there is only dev in the profile list, the module is activated.

    Module-Profile=!product

Module activation and Spring context

When a module is activated, the spring.profiles.active properties of its Spring context are configured synchronously, and the corresponding beans are activated. Example:

  1. Add the following configuration to the application.properties:

    com.alipay.sofa.boot.active-profiles=dev,test
  2. Add the following configuration to the sofa-module.properties file of the SOFABoot module: This indicates that the module will be activated when the profile list configured in Step 1 contains test or product. At this point, the module will be activated.

    Module-Profile=test,product
  3. If the module is activated, the workspace information spring.profiles.active of its Spring context is also set to dev,test. At this time, beans with beanId devBeanId and testBeanId will be activated.

<?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>