spring之Environment

spring 的Environment包含两方便的抽象,profile和 property

前者是一组bean的定义,只有相应的profile被激活的情况下才会起作用。

后者是提供方便的抽象,应用程序可以方便的访问 system property 环境变量自定义属性等。

Profile

想象这样一种情况,在软件开发的过程中有开发环境和正式环境,他们使用的数据源不同,怎么才能做到无缝切换呢。也就是说怎么让Spring容器在不同的条件下注册不同的Bean。比如说生产环境和测试的环境的数据源。

传统做法

在spring3.0以前我们可以这么做,把这些不同环境的bean定义在不同的xml文件中,然后采用import标签配合PropertySourcesPlaceholderConfigurer导入不同的配置。

如下一个示例:

<import resource="com/test/dao/dao-${test}.xml" />  

上面这个示例要使之工作,需要在spring容器启动之前在System property和环境变量中添加test. 不能使用PropertySourcesPlaceholderConfigurer,因为其在spring 容器加载bean定义后才加载。具体有以下几种方式:

1.     带参数启动。启动程序中传入property。如下:

-Dtest=test

2.     在程序中编码添加

// key 和value 均可以从配置文件读取

  String key = "test";  

  String value = "test";  

  Properties pp = System.getProperties();  

  pp.put(key, value);  

使用profile

以上的机制的好坏暂且不谈,但是有一点是不同的团队拥有不同的方案,而且配置文件分散,spring的profile提供统一的解决方案。

Profile是<beans>标签的一个属性,定义了一组bean属于同一个profile,如下定义了三组bean,隶属于不同的profile,可以看到beans标签可以嵌套,这样就可以把不同的配置放在一起。

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd"  
    >  
   
    <beans profile="qa">  
   
       <bean id="userDao" class="com.test.dao.UserDaoImp">  
       </bean>  
   
       <bean id="userDao1" class="com.test.dao.UserDaoImp">  
           <qualifier type="com.test.service.MyQualify" value="userMy" />  
       </bean>  
    </beans>  
   
    <beans profile="test">  
   
       <bean id="userDao" class="com.test.dao.UserDaoImp">  
       </bean>  
   
       <bean id="userDao1" class="com.test.dao.UserDaoImp">  
           <qualifier type="com.test.service.MyQualify" value="userMy" />  
       </bean>  
    </beans>  
     
     
    <beans profile="default">  
   
       <bean id="userDao" class="com.test.dao.UserDaoImp">  
       </bean>  
   
       <bean id="userDao1" class="com.test.dao.UserDaoImp">  
           <qualifier type="com.test.service.MyQualify" value="userMy" />  
       </bean>  
    </beans>  
</beans>

启用profile

要启用一个profile,可以有以下几种方式:

1.   使用代码

context.getEnvironment().setActiveProfiles("test");  

context.refresh();  

2.     使用默认的

如上节配置的profile,最后一个名字是default。

3.     启动时传入参数。

    -Dspring.profiles.active="profile1,profile2"

 

注解

 

有相关的注解@profile,,这也是一个元注解。用法见spring官方文档。

 

Property

 

Spring的Environment可以方便的访问property属性,包含系统属性,环境变量和自定义的。

 

并且是可配置的,可以加入自定义的property,这基于spring使用PropertySources 来管理不同的PropertySource

       ApplicationContext ctx = new GenericApplicationContext();  
       Environment env = ctx.getEnvironment();  
       boolean containsFoo = env.containsProperty("foo");  
       System.out.println("Does my environment contain the 'foo' property? " + containsFoo);  
        
       MutablePropertySources sources = context.getEnvironment().getPropertySources();  
       sources.addFirst(new MypropertySource()); 

当然可以使用使用注解@PropertySource

原文地址:https://www.cnblogs.com/zhangjianbin/p/6351627.html