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

环境与profile

好比,考虑一下数据库的配置java

配置类

@Profile基于激活的profile实现bean的装配web

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.xmlspring

<?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呢?sql

目前咱们的项目几乎都是使用:在构建阶段用maven的profile来肯定将哪一个配置编译到可部署的应用中,缺点在于要为每一个环境从新构建应用。数据库

激活profile

Spring在肯定哪一个profile处于激活状态时,须要依赖两个独立的属性maven

spring.profile.active
spring.profile.default

先判断spring.profile.active是否有值,若没有,再查看spring.profile.default。均没有,就不会激活profiletcp

多种方式来设置这两个属性测试

1.做为DispactcherServlet参数ui

2.做为web应用上下文参数spa

3.做为JNDI条目

4.做为环境变量

5.做为JVM的系统属性

6.在集成测试类中,使用@ActiveProfiles注解设置

具体列子,参考《Spring in action》第4版

相关文章
相关标签/搜索