Environment
环境在容器中是一个抽象的集合,是指应用环境的2个方面: profiles和 properties.

profile
配置是一个被命名的,bean定义的逻辑组,这些bean只有在给定的profile配置激活时才会注册到容器。不管是XML还是注解,Beans都有可能指派给profile配置。Environment环境对象的作用,对于profiles配置来说,它能决定当前激活的是哪个profile配置,和哪个profile是默认。

在所有的应用中,Properties属性扮演一个非常重要的角色,可能来源于一下源码变量:properties文件,JVM properties,system环境变量,JNDI, servlet context parameters上下文参数,专门的Properties对象,Maps等等。Environment对象的作用,对于properties来说,是提供给用户方便的服务接口,方便撰写配置、方便解析配置。

bean定义profiles是核心容器内的一种机制,该机制能在不同环境中注册不同的bean。环境的意思是,为不同的用户做不同的事儿,该功能在很多场景中都非常有用,包括:开发期使用内存数据源,在QA或者产品上则使用来自JNDI的相同的数据源开发期使用监控组件,当部署以后则关闭监控组件,是应用更高效为用户各自注册自定义bean实现.

考虑一个实际应用中的场景,现在需要一个DataSource。开测试环境中,这样配置:

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("my-schema.sql")
.addScript("my-test-data.sql")
.build();
}

  

现在想一想,如何将应用部署到QA或者生产环境,假设生产环境中使用的JNDI。我们的dataSource bean看起来像这样:

@Bean(destroyMethod="")
public DataSource dataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}

  

我们需要在某些上下文环境中使用某些bean,在其他环境中则不用这些bean。你也许会说,你需要在场景A中注册一组bean定义,在场景B中注册另外一组。先看看我们如何修改配置来完成此需求。
@Profile注解的作用,是在一个或者多个指定profiles激活的情况下,注册某个组件。使用上面的样例,重写dataSource配置:

@Configuration
@Profile("dev")
public class StandaloneDataConfig { @Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:com/bank/config/sql/schema.sql")
.addScript("classpath:com/bank/config/sql/test-data.sql")
.build();
}
}
@Configuration
@Profile("production")
public class JndiDataConfig { @Bean(destroyMethod="")
public DataSource dataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}
}

  

开启profile

要修改配置,我们仍然需要指定要激活哪个文件。如果现在运行上面的样例应用,它会抛异常NoSuchBeanDefinitionException,因为容器找不到dataSourcebean。

有多种方式激活配置,但是最直接的方式是编程式的方式使用ApplicationContext API

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev");
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh();

  

此外,还可以使用spring.profiles.active激活配置,该属性可以配置在系统环境变量、JVM系统属性、web.xml
中JNDI中的servlet context上下文参数
注意配置文件不是单选;可能会同时激活多个配置文件,编程式的使用方法setActiveProfiles(),该方法接收String数组参数,也就是多个配置文件名

ctx.getEnvironment().setActiveProfiles("profile1", "profile2");
//声明式的使用spring.profiles.active ,值可以为逗号分隔的配置文件名列表, -Dspring.profiles.active="profile1,profile2"

 

如果没有任何profile配置被激活,默认的profile将会激活。
默认profile配置文件可以更改,通过环境变量的setDefaultProfiles方法,或者是声明的spring.profiles.default属性值

Spring的环境抽象提供了用于检索一系列的property sources属性配置文件。

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

  

在上面的片段中,通过较高层次方式检索SPring是否在当前环境中定义了foo property属性。为了检索该属性,环境对象在一组PropertySource对象中执行检索。PropertySource是key-value键值对配置文件的抽象,Spring的StandardEnvironment
配置了2个PropertySource对象.其一是JVM系统properties(System.getProperties()),另一个是一组系统环境变量System.getEnv()。

@PropertySource注解提供了一个方便的方式,用于增加一个PropertySource到Spring的环境中: 给定一个文件"app.properties"包含了key/value键值对testbean.name=myTestBean,下面的@Configuration类使用了@PropertySource
,使用这种方式调用testBean.getName()将会返回myTestBean。

@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
Environment env; @Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}

  

转自:https://www.jianshu.com/p/49e950b0b008

spring Environment的更多相关文章

  1. Spring Environment(一)API 介绍

    Spring Environment(一)API 使用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 3. ...

  2. Spring Environment(二)源码分析

    Spring Environment(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  3. Spring Environment(三)生命周期

    Spring Environment(三)生命周期 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  4. Spring Environment抽象

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

  5. Spring Environment的加载

     这节介绍environment,默认环境变量的加载以及初始化.  之前在介绍spring启动过程讲到,第一步进行环境准备时就会初始化一个StandardEnvironment.下图为Environm ...

  6. Spring Environment对象获取属性

    String[] activeProfiles = env.getActiveProfiles();//获取当前是启用哪一个个配置文件 System.out.println(Arrays.toStri ...

  7. Spring中的Environment外部化配置管理详解

    Environment的中文意思是环境,它表示整个spring应用运行时的环境信息,它包含两个关键因素 profiles properties profiles profiles这个概念相信大家都已经 ...

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

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

  9. spring configuration 注解

    org.springframework.context.annotation @annotation.Target({ElementType.TYPE}) @annotation.Retention( ...

随机推荐

  1. Netty实战十之编解码器框架

    编码和解码,或者数据从一种特定协议的格式到另一种格式的转换.这些任务将由通常称为编解码器的组件来处理.Netty提供了多种组件,简化了为了支持广泛的协议而创建自定义的编解码器的过程.例如,如果你正在构 ...

  2. Java学习笔记之——final关键字

    最终的,最后的 可以修饰:变量.方法.类 (1) 修饰变量 不可改变,即常量,只能赋值一次,赋值之后不可改变 a. 修饰属性:private final int num; 要求属性要显式赋值 通常在属 ...

  3. Hibernate入门(七)一对多入门案例

    一对多 场景模拟:用户(一)对订单(多) 1.建表 创建客户表,字段有:客户id,客户姓名,客户性别,客户年龄,客户年纪,客户电话. 创建订单表,字段有:订单编号,明细编号,客户编号(外键) DROP ...

  4. 【读书笔记】iOS-库

    一,OS X和iOS自带一些标准的C程序库和操作系统相关的特殊性.在MAC和iOS的世界里,静态库采用.a扩展名(静态对象代码库存档),动态加载库采用.dylib扩展名.如果开发人员来自于Linux世 ...

  5. BDD实战篇 - .NET Core里跑Specflow - 可以跑集成测试和单元测试

    这是<如何用ABP框架快速完成项目 >系列中和DevOps系列文章其中一篇文章.   BDD很赞!比TDD先进很多,能够大大提高编码效率.   上一篇文章说了如何在.NET Core里安装 ...

  6. Elasticsearch Search APIs

    Elasticsearch Search APIs By:授客 QQ:1033553122 1. 搜索 1 在单个索引的所有类型中搜索 1 在单个索引的指定类型中搜索 1 在多个指定的索引中搜索 1 ...

  7. WebStorm File Watchers配置将.less文件编译后的.css输出至指定目录

    Arguments:其实是命令行输入“lessc file.less file.css”后者指定路径 Output Paths to refresh:刷新changed后.css文件

  8. leetcode-70.爬楼梯

    leetcode-70.爬楼梯 Points 斐波那契 动态规划 题意 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给 ...

  9. Hive之FAILED: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient异常

    一.场景 Hive启动不报错,当使用show functions;或create table...时报:FAILED: SemanticException org.apache.hadoop.hive ...

  10. 使用Visual Studio Team Services敏捷规划和项目组合管理(二)——VSTS中的工作项

    使用Visual Studio Team Services敏捷规划和项目组合管理(二)--VSTS中的工作项 1.通过project/team下拉菜单选择MyHealthClinic\Web,导航到W ...