环境与profile
比如,考虑一下数据库的配置
配置类
@Profile基于激活的profile实现bean的装配
import javax.sql.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;
import org.springframework.jndi.JndiObjectFactoryBean;
@Configuration
public class DataSourceConfig {
@Bean(destroyMethod = "shutdown")
@Profile("dev")
public DataSource embeddedDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sql")
.addScript("classpath:test-data.sql")
.build();
}
// 在QA环境中,你也可以选择完全不同的DataSource配置,配置为Common DBCP连接池
@Bean(destroyMethod = "shutdown")
@Profile("test")
public DataSource datasource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:h2:tcp://dbserver/~/test");
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUserName("sa");
dataSource.setPassword("password");
dataSource.setInitialsize(20);
dataSource.setMaxActive(30);
return dataSource;
}
@Bean
@Profile("prod")
public DataSource jndiDataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("jdbc/myDS");
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
return (DataSource) jndiObjectFactoryBean.getObject();
}
// 三种方法都返回了DataSource bean ,仅仅是策略不同而已
}
XML中配置profile
datasorce-config.xml
<?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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
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" type="H2">
<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"
lazy-init="true"
jndi-name="jdbc/myDatabase"
resource-ref="true"
proxy-interface="javax.sql.DataSource" />
</beans>
</beans>
那么问题来了,我们该怎么激活profile呢?
目前我们的项目几乎都是使用:在构建阶段用maven的profile来确定将哪个配置编译到可部署的应用中,缺点在于要为每个环境重新构建应用。
激活profile
Spring在确定哪个profile处于激活状态时,需要依赖两个独立的属性
spring.profile.active
spring.profile.default
先判断spring.profile.active是否有值,若没有,再查看spring.profile.default。均没有,就不会激活profile
多种方式来设置这两个属性
1.作为DispactcherServlet参数
2.作为web应用上下文参数
3.作为JNDI条目
4.作为环境变量
5.作为JVM的系统属性
6.在集成测试类中,使用@ActiveProfiles注解设置
具体列子,参考《Spring in action》第4版