1 配置profile bean
package com.myapp;
import javax.activation.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.profile;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@Configuration
@Profile("dev")
public class DevelopmentProfileConfig{
@Bean(destoryMethod="shutdown")
public DataSource dataSource(){
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
....
}
}
package com.myapp;
import javax.activation.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.profile;
import org.springframework.jndi.JndiObjectFactoryBean;
@Configuration
@Profile("prod")
public class ProductionProfileConfig {
@Bean
public DataSource dataSource(){
...
}
}
在上面的例子中只有相应的环境profile激活时,才会创建相应的bean.在Spring3.1中只能在类的级别上使用@Profile注解.从spring3.2版本以后,也可以在方法级别上使用@Profile注解,与Bean一同使用;
package com.myapp;
import javax.activation.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.profile;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@Configuration
public class DataSourceConfig {
@Bean(destoryMethod="shutdown")
@Profile("dev")
public DataSource embeddedDataSource(){
...
}
@Bean
@Profile("product")
public DataSource jndiDataSource(){
...
}
}
这里尽管每个datasource bean都被声明在profile中,但是只有相应的profile激活时才会被创建.而没有指定profile的bean始终都会被创建
2 在xml中指定profile
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
profile="dev">
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:test-data.sql">
</jdbc:embedded-database>
</beans>
与之类似,我们也可以将profile设置成product
重复使用beans元素指定多个profile
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<beans profile="dev">
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:test-data.sql">
</jdbc:embedded-database>
</beans>
<beans profile="prod">
<jee:jndi-lookup id="datasource"
jndi-name="jdbc/myDataBase"
resource-ref="true"
proxy-interface="javax.sql.DataSource"/>
</beans>
</beans>
3 激活profile
spring在确定激活哪个profile时依赖两个独立的属性:spring.profiles.active和spring.profiles.defaut;如果设置了spring.profiles.active那么它的值会用来确定激活哪个profile,如果则取spring.profile.default的值
可以通过如下方式来设置这两个属性:
作为DispatcherServlet的初始化参数
作为web应用的上下文参数
作为JNDI条目
作为环境变量
作为jvm的系统属性
在集成测试类上,使用@ActiveProfiles
在web.xml中设置默认的profile
<?xml version="1.0" encoding="UTF-8">
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfiguration</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<context-param>
<!-- 为上下文置默认的profile-->
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<!-- 为servlet设置默认的profile-->
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
....
使用profile进行测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(class={PersistenceTestConfig.class})
@ActiveProfiles("dev")
public class PersistenceTest{
...
}