Spring(19)——Profile(二)

简介: 19.2 指定启用的profile 前面已经介绍了profile的指定,我们知道指定了profile后则表示对应的内容只有在特定的profile下才会生效。当前应用究竟使用的是哪个profile,或者是哪些profile,这是需要我们来指定的。

19.2 指定启用的profile

前面已经介绍了profile的指定,我们知道指定了profile后则表示对应的内容只有在特定的profile下才会生效。当前应用究竟使用的是哪个profile,或者是哪些profile,这是需要我们来指定的。说的专业一点就叫激活,即只有处于激活状态的profile对应的定义才会生效,当然也包括那些没有指定profile的定义。

在Spring中激活哪个profile是通过参数spring.profiles.active来指定的,我们可以把它定义为一个系统环境变量、JVM参数,或者是在web.xml中的一个ServletContext参数。如下就是通过JVM参数指定激活的profile为dev的示例。

-Dspring.profiles.active=dev

如下是通过在web.xml文件中通过ServletContext的参数指定激活的profile的示例,其激活的profile是dev。

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

当然,我们也可以同时激活多个profile,同时激活多个profile时,多个profile之间以逗号隔开。如下示例即表示同时激活dev和production两个profile。(其它如JVM参数指定等是同样的规则)

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

除了使用spring.profiles.active参数进行指定外,我们还可以通过在程序中动态的指定激活的profile。如下示例中我们就通过获取当前ApplicationContext的Environment对象,然后通过该对象指定激活的profile为production。使用程序指定激活的profile时需要注意先构建一个空的ApplicationContext对象,然后再通过该对象的Environment对象指定激活的profile,再指定对应的bean定义对应的资源位置,最后通过调用refresh()方法让ApplicationContext对象解析对应的bean定义。

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

如果需要同时指定多个激活的profile,则可以给setActiveProfiles()方法指定多个参数,其对应的参数是一个可变参数。

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

19.3 默认profile

此外,我们还可以给我们的应用指定默认的profile。我们知道如果一个<beans/>没有指定profile,且其上级的<beans/>也没有指定profile,那么对应<beans/>中定义的所有的bean无论激活的何种profile,它们都是可用的。而默认profile的概念是我们定义一个默认的profile,然后如果一个<beans/>指定的profile为默认的profile,则当没有激活的profile时,对应默认profile的<beans/>中定义的bean都是可用的,但是一旦有激活的profile,那么对应默认profile的<beans/>就是不可用的。如果我们默认的bean定义不指定profile的话,那么对应的bean定义将在所有的情况下都是可用的,一旦我们改变profile,那么可能就会存在两个相同类型的bean定义。又或者我们将默认的bean定义与特定的bean定义定义为不同的两个profile,这样的结果是我们必须指定一个激活的profile。所以说默认profile这种机制也是非常有用的,即我们可以通过默认profile来定义默认的bean定义,然后通过改变profile来改变对应的bean定义。

Spring中默认profile的名称是“default”,即默认情况下我们将一个<beans/>的profile指定为default,即表示其对应默认的profile。如下示例中我们定义了在没有处于激活状态的profile时hello_default是可用的,而在名称为production的profile处于激活状态时hello_production是可用的。

<!-- 只有在激活了名称为production的profile时其中定义的bean才是可用的 -->
<beans profile="production">
	<bean id="hello_production" class="com.app.Hello"/>
</beans>

<!-- 默认profile,即只有在没有激活任何profile的情况下其中定义的bean才是可用的 -->
<beans profile="default">
	<bean id="hello_default" class="com.app.Hello"/>
</beans>

19.3.1 更改默认profile的名称

默认profile的名称是“default”,我们也可以通过spring.profiles.default参数进行更改,更改方式类似于通过参数spring.profiles.active指定激活的profile。

1、如下是通过JVM参数指定默认的profile为production。

	-Dspring.profiles.default=production

2、如下是通过ServletContext的参数指定默认的profile为production(供ContextLoaderListener使用)。

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

对于这种直接通过参数spring.profiles.default指定默认profile的情况,我们也可以同时指定多个profile,多个profile之间以逗号隔开。

3、也可以通过程序化的方式获取ApplicationContext对应的Environment对象,然后通过该对象设置对应的默认profile。如下示例表示我们设置默认的profile为“default”和“production”。setDefaultProfiles()方法接收的是一个可变参数,所以我们可以同时指定多个默认的profile。

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

(注:本文是基于Spring4.1.0所写)

目录
相关文章
|
1月前
|
Java 测试技术 数据库
SpringBoot:@Profile注解和Spring EL
SpringBoot:@Profile注解和Spring EL
|
7月前
|
Java 应用服务中间件 Maven
解析Spring Boot中的Profile:配置文件与代码的双重掌控
解析Spring Boot中的Profile:配置文件与代码的双重掌控
|
4月前
|
Java Spring 容器
Spring注解驱动开发三切换环境Profile
Spring注解驱动开发三切换环境Profile
29 0
|
4月前
|
XML Java 应用服务中间件
spring和maven(profile)的多环境部署
spring和maven(profile)的多环境部署
47 0
|
7月前
|
Java 测试技术 Spring
Spring @Profile注解使用和源码解析
在之前的文章中,写了一篇使用Spring @Profile实现开发环境,测试环境,生产环境的切换,之前的文章是使用SpringBoot项目搭建,实现了不同环境数据源的切换,在我们实际开发中,会分为dev,test,prod等环境,他们之间数独立的,今天进来详解介绍Spring @Profile的原理。
49 0
|
8月前
|
Java Spring
Spring Boot入门(八) 之Profile多环境支持
Spring Boot入门(八) 之Profile多环境支持
|
10月前
|
Java 测试技术 数据库
spring Profile
spring Profile
|
10月前
|
Java Nacos Spring
使用Spring Boot的Profile功能来实现不同环境使用不同的Nacos Namespace的配置
使用Spring Boot的Profile功能来实现不同环境使用不同的Nacos Namespace的配置
386 1
|
10月前
|
Java Spring 容器
spring @Profile
spring @Profile
|
10月前
|
Java 测试技术 Spring
spring web设置profile
spring web设置profile