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 ]Is Locally Controlled的局限性

    一.在有机器人的游戏中,就不能使用IsLocallyControlled判断.而是要使用这个方法: 二.因为机器人也是属于本地控制的角色.所以不能使用IsLocallyControlled判断是否是本 ...

  2. [UE4]模拟网络延迟和丢包

    进入游戏界面,按下键盘的“~”键,打开控制台界面,输入net会自动列出跟网络相关的命令 1.Net pktLag=,模拟延迟,单位是毫秒 2.Net PktLagVariance=300,在模拟延迟的 ...

  3. Kafka分布式集群部署

    这个是kafka的官网地址:http://kafka.apache.org/ 1.kafka是一个消息系统. 2.kafka对流数据可以高效的实时处理. 3.分布式集群的环境下能够保证数据的安全. k ...

  4. HDFS操作及小文件合并

    小文件合并是针对文件上传到HDFS之前 这些文件夹里面都是小文件 参考代码 package com.gong.hadoop2; import java.io.IOException; import j ...

  5. 自然语言处理领域重要论文&资源全索引

    自然语言处理(NLP)是人工智能研究中极具挑战的一个分支.随着深度学习等技术的引入,NLP领域正在以前所未有的速度向前发展.但对于初学者来说,这一领域目前有哪些研究和资源是必读的?最近,Kyubyon ...

  6. TextView右上角显示小红点,小红点根据TextView的长度移动,小红点被TextView挤出去不显示的问题;

    大概就是图片这个样,这个功能很常见,本来我以为很简单,谁知道真的很简单: 遇到点小问题,记录一下,哈哈: 小红点的Drawable: <?xml version="1.0" ...

  7. replace实现替换全部

    方法: string.replace(new RegExp(oldString,"gm"),newString)) gm g=global, m=multiLine , 大致上方法 ...

  8. 安全测试6_Web安全工具第二节(代理抓包分析工具)

    上节课讲了浏览器及扩展,这节课继续来学习下抓包分析. 首先看下下图,了解下代理工具的原理:代理就相当于收费站一样,任何要通过的车辆必须经过它. 浏览器的代理我们可以通过设置进行手动设置代理,或者通过P ...

  9. 使用python操作excel表格

    首先 pip install xlrd  安装相关模块 其次:使用方法: 1 导入模块 import xlrd 2 打开excel文件读取数据 worksheet=xlrd.open_workbook ...

  10. [Unity基础]RenderTexture

    参考链接: https://www.cnblogs.com/Jimm/p/5951362.html 一.相关API 1.Texture2D.ReadPixels 从RenderTexture.acti ...