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. Jmeter(三十六)纵横并发、限制QPS

    一.纵横并发 Jmeter设计并发事件,这应该是一项必备技能. 首先来看并发的概念. 通常在性能测试中会涉及到并发用户数的概念,有关并发用户数(系统用户数)的详解后续再记. (有关并发.并行的概念参考 ...

  2. js原生面向对象-仿layui选项卡

    喜欢琢磨,给大家分享小编自己封装的仿layui的选项卡. <!DOCTYPE html> <html lang="en"> <head> < ...

  3. SQL Server 查询性能优化——创建索引原则(一)

    索引是什么?索引是提高查询性能的一个重要工具,索引就是把查询语句所需要的少量数据添加到索引分页中,这样访问数据时只要访问少数索引的分页就可以.但是索引对于提高查询性能也不是万能的,也不是建立越多的索引 ...

  4. 零基础学习python_文件(28-30课)

    本人小白一枚,随着现在对测试要求越来越高,动不动就要去会一门编程语言,没办法只能学习学习Python,今天看到几个月前还是菜鸟的人突然就已经能使用Python写简单系统了,没办法,虽然之前也简单学习过 ...

  5. Linux下安装PHP+Nginx+Msql

    安装Nginx: 1.先指定个文件存放位置  usr/local/src 2. 下载nginx,  wget http://nginx.org/download/nginx-1.12.0.tar.gz ...

  6. pyautogui控制鼠标键盘自动填写数据

    import os import pyautogui import time, os import pyperclip # 复制 pyautogui.FAILSAFE = False class Au ...

  7. redis下操作hash对象

    redis下操作hash对象 hash用于存储对象,对象的格式为键值对 命令 设置 设置单个属性 HSET key field value 设置多个属性 HMSET key field value [ ...

  8. 【Python爬虫实战】多线程爬虫---糗事百科段子爬取

    多线程爬虫:即程序中的某些程序段并行执行,合理地设置多线程,可以让爬虫效率更高糗事百科段子普通爬虫和多线程爬虫分析该网址链接得出:https://www.qiushibaike.com/8hr/pag ...

  9. 练手mysqlbinlog日志恢复数据(centos6.5 64,mysql5.1)

    练手mysql bin log日志相关 系统是centos 6.5 64 阿里云的服务器 mysql版本5.1 1 如何开启bin-log日志? vi /etc/my.cnf [mysqld] log ...

  10. py库: scrapy (深坑未填)

    scrapy 一个快速高级的屏幕爬取及网页采集框架 http://scrapy.org/ 官网 https://docs.scrapy.org/en/latest/ Scrapy1.4文档 http: ...