Module activation
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.propertiesfile for the deployable module. Deployable modules are modules that usespring-boot-maven-pluginto packagefat jar, such as the web module in the SOFABoot web project.Each module has its own
sofa-module.propertiesfile. 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:
Configure the properties file of a deployable module
Add the following key-value pairs to the
application.propertiesfile.Key:
com.alipay.sofa.boot.active-profilesValue: 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.
Configure the
sofa-module.propertiesfile of the SOFABoot moduleConfigure 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:
! devrepresentationcom.alipay.sofa.boot.active-profilesis activated when dev is not included.
Activation example
Configure a single profile
An example of the preceding steps is as follows:
Add the following configuration to the
application.properties: This indicates that dev can be activated.com.alipay.sofa.boot.active-profiles=devAdd the following configuration to the
sofa-module.propertiesfile 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:
Add the following configuration to the
application.properties: indicating that dev and test can be activated.com.alipay.sofa.boot.active-profiles=dev,testAdd the following configuration to the
sofa-module.propertiesfile 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:
Add the following configuration to the
application.properties: This indicates that dev can be activated.com.alipay.sofa.boot.active-profiles=devAdd the following configuration to the
sofa-module.propertiesfile 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:
Add the following configuration to the
application.properties:com.alipay.sofa.boot.active-profiles=dev,testAdd the following configuration to the
sofa-module.propertiesfile 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,productIf the module is activated, the workspace information
spring.profiles.activeof its Spring context is also set todev,test. At this time, beans with beanIddevBeanIdandtestBeanIdwill 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>