高级装配小笔记--环境与profile

环境与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版

转载于:https://my.oschina.net/grittan/blog/3010057

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值