Spring(19)——Profile(二)

19.2 Specifying enabled profiles

The specification of the profile has been introduced before. We know that after the profile is specified, it means that the corresponding content will only take effect under a specific profile. Which profile, or which profile, is currently used by the application needs to be specified by us. The professional point is called activation, that is, only the definition corresponding to the active profile will take effect, of course, including those definitions that do not specify a profile.

Which profile is activated in Spring is specified by the parameter spring.profiles.active, we can define it as a system environment variable, JVM parameter, or a ServletContext parameter in web.xml. The following is an example of specifying the active profile as dev through JVM parameters.

-Dspring.profiles.active=dev

The following is an example of the activated profile specified by the parameter of the ServletContext in the web.xml file, and the activated profile is dev.

<context-param>
	<param-name>spring.profiles.active</param-name>
	<param-value>dev </param-value>
</context-param>

Of course, we can also activate multiple profiles at the same time. When multiple profiles are activated at the same time, the multiple profiles are separated by commas. The following example indicates that both dev and production profiles are activated at the same time. (Others such as JVM parameter specification are the same rules)

<context-param>
	<param-name>spring.profiles.active</param-name>
	<param-value>dev,production</param-value>
</context-param>

In addition to specifying using the spring.profiles.active parameter, we can also dynamically specify the active profile in the program. In the following example, we obtain the Environment object of the current ApplicationContext, and then specify the active profile as production through this object. When using the program to specify the activated profile, you need to pay attention to constructing an empty ApplicationContext object first, then specify the activated profile through the Environment object of the object, then specify the corresponding resource location of the corresponding bean definition, and finally let the ApplicationContext by calling the refresh() method. The object resolves the corresponding bean definition.

	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
	context.getEnvironment().setActiveProfiles("production");
	context.setConfigLocation("classpath:applicationContext.xml");
	context.refresh();

If you need to specify multiple active profiles at the same time, you can specify multiple parameters for the setActiveProfiles() method, and the corresponding parameter is a variable parameter.

	context.getEnvironment().setActiveProfiles("dev","production");

19.3 Default profile

In addition, we can also specify a default profile for our application. We know that if a <beans/>profile is not specified, and its superior <beans/>does not specify a profile, then <beans/>all beans defined in the corresponding are available no matter what profile is activated. The concept of the default profile is that we define a default profile, and then if a <beans/>specified profile is the default profile, when there is no active profile, <beans/>the beans defined in the corresponding default profile are available, but once activated profile, then the corresponding default profile <beans/>is unavailable. If our default bean definition does not specify a profile, then the corresponding bean definition will be available in all cases, and once we change the profile, there may be two bean definitions of the same type. Or we define the default bean definition and the specific bean definition as two different profiles, the result is that we must specify an active profile. So the default profile mechanism is also very useful, that is, we can define the default bean definition through the default profile, and then change the corresponding bean definition by changing the profile.

The name of the default profile in Spring is "default", that is, by default, we <beans/>specify a profile as default, which means that it corresponds to the default profile. In the following example we define that hello_default is available when there is no active profile, and hello_production is available when the profile named production is active.

<!-- beans defined in a profile named production are only available when the profile named production is activated --> 
< beans  profile = " production " >
	<bean id="hello_production" class="com.app.Hello"/>
</beans>

<!-- Default profile, i.e. beans defined in it are only available if no profile is activated --> 
< beans  profile = " default " >
	<bean id="hello_default" class="com.app.Hello"/>
</beans>

19.3.1 Changing the name of the default profile

The name of the default profile is "default", and we can also change it through the spring.profiles.default parameter, which is similar to specifying the active profile through the parameter spring.profiles.active.

1. The following is to specify the default profile as production through JVM parameters.

	-Dspring.profiles.default=production

2. The following is to specify the default profile as production (used by ContextLoaderListener) through the parameters of ServletContext.

<context-param>
	<param-name>spring.profiles.default</param-name>
	<param-value>production</param-value>
</context-param>

For this situation where the default profile is specified directly through the parameter spring.profiles.default, we can also specify multiple profiles at the same time, and multiple profiles are separated by commas.

3. You can also programmatically obtain the Environment object corresponding to the ApplicationContext, and then set the corresponding default profile through the object. The following example shows that we set the default profiles to "default" and "production". The setDefaultProfiles() method receives a variable parameter, so we can specify multiple default profiles at the same time.

	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
	context.getEnvironment().setDefaultProfiles("production", "default");
	context.setConfigLocation("classpath:applicationContext.xml");
	context.refresh();

(Note: This article is written based on Spring 4.1.0)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326336320&siteId=291194637