spring profile

第三章:1.bean的高级装配

profile:  开发环境(dev),QA环境(qa),生产环境(prod)

1.配置profile bean:

1.1基于注解的形式:

@Profile注解可以指定某个bean属于哪个profile,Spring3.1时@Profile注解只能加在类上,Spring3.2开始可以加在方法上
@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();
  }
  @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();
  }
}

 

1.2.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="qa">
    <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:url=""
          p:driverClassName=""
          p:username=""
    />
  </beans>
</beans>

 

2.如何激活?

两个属性:spring.profiles.default和spring.profiles.active

spring首先找active的profile,若没有则找default,若都没有则没有激活的profile。

有多种方式来设置这两个属性:

  • 作为DispatcherServlet的初始化参数;
  • 作为web应用的上下文参数;
  • 作为JNDI条目;
  • 作为环境变量;
  • 作为JVM的系统属性;
  • 在集成测试类上,使用@ActiveProfiles注解配置。

 

1.作为DispatcherServlet的初始化参数init-param
<!-- 在上下文中设置profile的默认值 -->
16   <context-param>
17       <param-name>spring.profiles.default</param-name>
18       <param-value>dev</param-value>
19   </context-param>


21 <listener> 22 <listener-class> 23 org.springframework.web.context.ContextLoaderListener 24 </listener-class> 25 </listener> 26 27 <servlet> 28 <servlet-name>appServlet</servlet-name> 29 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 30 <!-- 在servlet中设置profile的默认值 --> 31 <init-param> 32     <param-name>spring.profiles.default</param-name> 33     <param-value>dev</param-value> 34 </init-param> 35 <load-on-startup>1</load-on-startup> 36 </servlet>

37 <servlet-mapping> 38 <servlet-name>appServlet</servlet-name> 39 <url-pattern>/</url-pattern> 40 </servlet-mapping>

使用profile进行测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestConfig.class)
@ActiveProfiles("dev")
public class MagicExistsTest {
........
}