高級裝配小筆記--環境與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版

相關文章
相關標籤/搜索