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

结束

本文中使用的代码继承之前,且改动较小,故不再上传。至此,spring 的核心容器 模块 的内容,基本介绍完毕,剩下的诸如国际化、容器事件,不在介绍,有兴趣的话自行研究。

spring之Environment的更多相关文章

  1. Spring系列.Environment接口

    Environment 接口介绍 在 Spring 中,Environment 接口主要管理应用程序两个方面的内容:profile 和 properties. profile 可以简单的等同于环境,比 ...

  2. Spring Environment抽象

    1:概述 Spring中Environment是Spring3.1版本引入的,是Spring核心框架定义的一个接口,用来表示整个应用运行时环境.该环境模型只接受两种应用环境profiles(配置文件) ...

  3. Spring系列15:Environment抽象

    本文内容 Environment抽象的2个重要概念 @Profile 的使用 @PropertySource 的使用 Environment抽象的2个重要概念 Environment 接口表示当前应用 ...

  4. spring源码分析之<context:property-placeholder/>和<property-override/>

    在一个spring xml配置文件中,NamespaceHandler是DefaultBeanDefinitionDocumentReader用来处理自定义命名空间的基础接口.其层次结构如下: < ...

  5. 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)

    前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...

  6. 【译】Spring 4 @PropertySource和@Value注解示例

    前言 译文链接:http://websystique.com/spring/spring-propertysource-value-annotations-example/ 本篇文章将展示如何通过@P ...

  7. Spring Boot启动流程详解(一)

    环境 本文基于Spring Boot版本1.3.3, 使用了spring-boot-starter-web. 配置完成后,编写了代码如下: @SpringBootApplication public ...

  8. Spring Boot文档阅读

    原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...

  9. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

随机推荐

  1. [UE4]Canvas Panel

    一.Canvas Panel:画布.Canvas Panel中内的元素可以任何摆放位置.Canvas Panel是UserWiget默认的根节点容器,可以把跟节点删除替换生成任何的UI元素. 二.选择 ...

  2. vue 数据绑定 绑定属性 循环渲染数据

    <template> <!-- vue的模板里面 所有的内容要被一个根节点包含起来 --> <div id="app"> <h2>{ ...

  3. win10使用4G 模块RNDIS模式上网

    Windons使用RNDIS模式上网步骤 Chapter 1 模块端配置 1模块设置为RNDIS模式 1.       以EC20CEFAG模块为例 2.       命令如下: 1)         ...

  4. PROC UNIVARIATE 简单示例

    OPTIONS NOCENTER LS=MAX PS=MAX USER=SASHELP; PROC UNIVARIATE DATA=FISH;WHERE SPECIES='Bream';VAR HEI ...

  5. C#列的一些操作

    //变更列名 DataTableA.Columns["原来的列名"].ColumnName="新的列名";

  6. c# 未能加载文件或程序集

    最近做项目时碰到这个问题了,goole.百度了半天,整理了以下几种可能: DLL文件名与加载时的DLL文件名不一致, DLL文件根本不存在,即出现丢失情况, 加载DLL路径错误,即DLL文件存在,但加 ...

  7. (转)C#读写共享文件

    原文地址:https://www.cnblogs.com/guohu/p/4694515.html using System; using System.Collections.Generic; us ...

  8. SPSS中文版安装

    我这里安装的是64位的 双击安装包 选择您的安装目录 在这里等几分钟 重要!安装完毕后将图中选项勾去掉! 这个时候输入一下授权码 9DNCAF2O3QVDV7FBIO696OO6GWLNXZPPRYT ...

  9. RESTframwork之视图view

    一 在view.py 中: class AuthorView(APIView): def get(self, request): author_list = Author.objects.all() ...

  10. mysql-5.7.16-winx64解压版安装超详细图文教程

    1.安装: 将下载的mysql-5.7.16-winx64压缩包解压后的整个目录放在自己喜欢的位置,我的放在D盘根目录下 2.配置: 进入mysql-5.7.16-winx64目录,将里面的my-de ...