Spring-profile设置

开发环境和生产环境通常采用不同的数据库连接方式,开发环境可以采用侵入式,而生产环境中采用jndi连接池,所以要根据不同环境配置不同的bean,Spring中提供了profile来实现动态生成相应的bean:

javaConfig方式:

 1 package com.myapp;
 2 
 3 import javax.activation.DataSource;
 4 
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.ComponentScan;
 7 import org.springframework.context.annotation.Configuration;
 8 import org.springframework.context.annotation.Profile;
 9 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
10 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
11 import org.springframework.jndi.JndiObjectFactoryBean;
12 @Configuration
13 @ComponentScan
14 public class DataSourceConfig {
15 @Bean(destroyMethod="shutdown")
16 @Profile("dev")
17     public DataSource EmbeddedDataSource(){
18         return (DataSource) new EmbeddedDatabaseBuilder()
19                 .setType(EmbeddedDatabaseType.H2)
20                 .addScript("classpath:xxx.sql")
21                 .addScript("classpath:yyy.sql")
22                 .build();
23     }
24 
25 @Bean
26 @Profile("prod")    
27     public DataSource jndiDataSource(){
28         JndiObjectFactoryBean jofb=new JndiObjectFactoryBean();
29         jofb.setJndiName("jndi/iDS");
30         jofb.setResourceRef(true);
31         jofb.setProxyInterface(xxx.class);
32         return (DataSource) jofb.getObject();
33     }
34 }

xml方式:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:c="http://www.springframework.org/schema/c"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 8     xmlns:jee="http://www.springframework.org/schema/jee"
 9     xmlns:context="http://www.springframework.org/schema/context"
10     xsi:schemaLocation="http://www.springframework.org/schema/beans 
11     http://www.springframework.org/schema/beans/spring-beans.xsd
12     http://www.springframework.org/schema/context 
13     http://www.springframework.org/schema/context/spring-context.xsd
14     http://www.springframework.org/schema/jdbc 
15     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
16     http://www.springframework.org/schema/jee 
17     http://www.springframework.org/schema/jee/spring-jee.xsd">
18     
19     <beans profile="dev">
20         <jdbc:embedded-database id="dataSource">
21             <jdbc:script location="classpath:xxx.sql"/>
22             <jdbc:script location="classpath:yyy.sql"/>
23         </jdbc:embedded-database>
24     </beans>
25     <beans profile="prod">
26         <jee:jndi-lookup jndi-name="jdbc/myDatabase" resource-ref="true" proxy-interface="javax.sql.DataSource"/>
27     </beans>
28 </beans>

通过profile标记不同的环境,但是如何激活它呢,可以通过设置spring.profiles.active和spring.profiles.default。如果设置了active,default便失去了作用。如果两个都没有设置,那么带有profiles的bean都不会生成。

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

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

以前两种方式举例,它们都可以在web.xml中设置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   <context-param>
12       <param-name>applicationContext</param-name>
13       <param-value>/applicationContext.xml</param-value>
14   </context-param>
15   <!-- 在上下文中设置profile的默认值 -->
16   <context-param>
17       <param-name>spring.profiles.default</param-name>
18       <param-value>dev</param-value>
19   </context-param>
20   
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>
41 </web-app>

这里都默认为dev环境,也就是说,@Profile("prod")注解字样的bean不会出现。

posted @ 2016-10-23 14:59  舒山  阅读(18631)  评论(0编辑  收藏  举报