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. .net core identity(一)简单运用

    1.net core identity涉及到很多知识,很多概念包括Claims,Principal等等概念需要我们一步步学习才能掌握其原理,有两篇博客是比较好的介绍该框架的, https://segm ...

  2. 在线学习和在线凸优化(online learning and online convex optimization)—基础介绍1

    开启一个在线学习和在线凸优化框架专题学习: 1.首先介绍在线学习的相关概念 在线学习是在一系列连续的回合(rounds)中进行的: 在回合,学习机(learner)被给一个question:(一个向量 ...

  3. Linux在线安装git

    一.先检测是否已安装git#rpm -qa | grep zlib-devel 二.提前 安装gcc#yum install gcc 三.具体安装命令 Git下载路径:https://www.kern ...

  4. [SQL]卸载数据库清理注册表方法regedit

    .打开注册表. 开始——运行——regedit——确定 .然后找到下面的文件夹,删除掉: HKEY_CURRENT_USER\ Software\ Microsoft\ Microsoft SQL S ...

  5. python学习笔记之二

    1.python计算运行时间 方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datet ...

  6. gradle本地、远程仓库配置

    本地仓库配置 配置环境变量GRADLE_USER_HOME,并指向你的一个本地目录,用来保存Gradle下载的依赖包. 远程仓库配置 一般Gradle.maven从中央仓库mavenCentral() ...

  7. postgre 查询同表中的,该节点写的所有子节点

    SELECT catalogid, foldername, parentid, folderpath FROM public.ic_catalog; --查询同表中的,该节点写的所有子节点 WITH ...

  8. 数据库设计和ER模型-------之ER模型的基本概念(第二章)

    ER模型(实体联系模型)的基本元素 实体:是一个数据对象,在ER模型中,实体用方框表示,方框内注明实体的名称 联系:表示一个或多个实体之间的关联关系,联系用菱形框表示,并用线段将其与相关的实体联系起来 ...

  9. MySQL----ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

    1.问题描述 在导入同事提供的一个sql文件时候,出现了一个1071错误,总结学习一下: 2.分析问题 错误的字面意思是说mysql字段设置的值太长了,不能大于767个字节,在网上找了一些资料后才知道 ...

  10. 对datetime日期类型进行序列化的处理

    datetime类型序列化 在工作中遇到从数据库中取出来一个datetime类型的数据,在对其进行序列化的过程中,报错python datetime.datetime is not JSON  ser ...