Spring之Environment类详解
一、Environment接口
先上源码:
package org.springframework.core.env;/*** Interface representing the environment in which the current application is running.* Models two key aspects of the application environment: <em>profiles</em> and* <em>properties</em>. Methods related to property access are exposed via the* {@link PropertyResolver} superinterface.** <p>A <em>profile</em> is a named, logical group of bean definitions to be registered* with the container only if the given profile is <em>active</em>. Beans may be assigned* to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema* or the {@link org.springframework.context.annotation.Profile @Profile} annotation for* syntax details. The role of the {@code Environment} object with relation to profiles is* in determining which profiles (if any) are currently {@linkplain #getActiveProfiles* active}, and which profiles (if any) should be {@linkplain #getDefaultProfiles active* by default}.** <p><em>Properties</em> play an important role in almost all applications, and may* originate from a variety of sources: properties files, JVM system properties, system* environment variables, JNDI, servlet context parameters, ad-hoc Properties objects,* Maps, and so on. The role of the environment object with relation to properties is to* provide the user with a convenient service interface for configuring property sources* and resolving properties from them.** <p>Beans managed within an {@code ApplicationContext} may register to be {@link* org.springframework.context.EnvironmentAware EnvironmentAware} or {@code @Inject} the* {@code Environment} in order to query profile state or resolve properties directly.** <p>In most cases, however, application-level beans should not need to interact with the* {@code Environment} directly but instead may have to have {@code ${...}} property* values replaced by a property placeholder configurer such as* {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer* PropertySourcesPlaceholderConfigurer}, which itself is {@code EnvironmentAware} and* as of Spring 3.1 is registered by default when using* {@code <context:property-placeholder/>}.** <p>Configuration of the environment object must be done through the* {@code ConfigurableEnvironment} interface, returned from all* {@code AbstractApplicationContext} subclass {@code getEnvironment()} methods. See* {@link ConfigurableEnvironment} Javadoc for usage examples demonstrating manipulation* of property sources prior to application context {@code refresh()}.** @author Chris Beams* @since 3.1* @see PropertyResolver* @see EnvironmentCapable* @see ConfigurableEnvironment* @see AbstractEnvironment* @see StandardEnvironment* @see org.springframework.context.EnvironmentAware* @see org.springframework.context.ConfigurableApplicationContext#getEnvironment* @see org.springframework.context.ConfigurableApplicationContext#setEnvironment* @see org.springframework.context.support.AbstractApplicationContext#createEnvironment*/
public interface Environment extends PropertyResolver {/*** Return the set of profiles explicitly made active for this environment. Profiles* are used for creating logical groupings of bean definitions to be registered* conditionally, for example based on deployment environment. Profiles can be* activated by setting {@linkplain AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME* "spring.profiles.active"} as a system property or by calling* {@link ConfigurableEnvironment#setActiveProfiles(String...)}.* <p>If no profiles have explicitly been specified as active, then any* {@linkplain #getDefaultProfiles() default profiles} will automatically be activated.* @see #getDefaultProfiles* @see ConfigurableEnvironment#setActiveProfiles* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME*/String[] getActiveProfiles();/*** Return the set of profiles to be active by default when no active profiles have* been set explicitly.* @see #getActiveProfiles* @see ConfigurableEnvironment#setDefaultProfiles* @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME*/String[] getDefaultProfiles();/*** Return whether one or more of the given profiles is active or, in the case of no* explicit active profiles, whether one or more of the given profiles is included in* the set of default profiles. If a profile begins with '!' the logic is inverted,* i.e. the method will return {@code true} if the given profile is <em>not</em> active.* For example, {@code env.acceptsProfiles("p1", "!p2")} will return {@code true} if* profile 'p1' is active or 'p2' is not active.* @throws IllegalArgumentException if called with zero arguments* or if any profile is {@code null}, empty, or whitespace only* @see #getActiveProfiles* @see #getDefaultProfiles* @see #acceptsProfiles(Profiles)* @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)}*/@Deprecatedboolean acceptsProfiles(String... profiles);/*** Return whether the {@linkplain #getActiveProfiles() active profiles}* match the given {@link Profiles} predicate.*/boolean acceptsProfiles(Profiles profiles);}
然后,进行讲解:
Enviroment包括两方面内容:profiles和properties。
- profiles
关于profiles的作用,需要先研究@Profile注解的用法。Evnironment与profiles的关系是Environment存储着哪些profiles是激活状态的,以及哪些profiles应该被默认激活。
其定义的前两个方法我们可以看到上面两点作用:
String[] getActiveProfiles();
String[] getDefaultProfiles();
由此可知,Environment是用于获取和管理profiles的一个类。
- properties
properties包括了如下几个方面:
properties配置文件;
JVM系统属性;
系统环境变量;
servlet context参数;
… …
一切参数类数据,都有Environment管理。Envrionment为用户提供了便捷的接口服务,来配置和解析properties相关的内容。
可以通过实现EnvironmentAware接口,来直接查询配置文件状态或解析属性。
大多数情况下,我们不会直接和Enviornment进行交互,而是通过${…}占位符,来设定相应的属性,然后通过PropertySourcesPlaceholderConfigurer来进行解析,PropertySourcesPlaceholderConfigurer类实现了EnvironmentAware接口,将解析后的数据,塞入Environment中去。
二、ConfigurableEnvironment接口
该接口更为全面,我们看其定义的方法;
- 设置profiles:
void setActiveProfiles(String... profiles);
- 添加profiles:
void addActiveProfile(String profile);
- 设置默认profiles:
void setDefaultProfiles(String... profiles);
- 获取MutablePropertySources:
MutablePropertySources getPropertySources();
- 获取系统属性:
Map<String, Object> getSystemProperties();
- 获取系统环境变量:
Map<String, Object> getSystemEnvironment();
Spring之Environment类详解相关推荐
- Spring之Joinpoint类详解
说明 Joinpoint是AOP的连接点.一个连接点代表一个被代理的方法.我们从源码角度看连接点有哪些属性和功能. 源码 /** Copyright 2002-2016 the original au ...
- SpringSecurity权限管理框架系列(六)-Spring Security框架自定义配置类详解(二)之authorizeRequests配置详解
1.预置演示环境 这个演示环境继续沿用 SpringSecurit权限管理框架系列(五)-Spring Security框架自定义配置类详解(一)之formLogin配置详解的环境. 2.自定义配置类 ...
- Spring Cloud限流详解(附源码)
在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...
- spring框架 AOP核心详解
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...
- JDBC学习笔记01【JDBC快速入门、JDBC各个类详解、JDBC之CRUD练习】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- Java中的Runtime类详解
Java中的Runtime类详解 1.类注释 /**Every Java application has a single instance of class Runtime that allows ...
- Spring Cloud限流详解(内含源码)
为什么80%的码农都做不了架构师?>>> 原文:http://www.itmuch.com/spring-cloud-sum/spring-cloud-ratelimit/ 在 ...
- Spring的注入方式详解
[html] view plaincopy Spring的注入方式详解 Spring有三个注入方式,type1,type2,type3 type1 接口依赖 type2 setter/getter ...
- Spring包含JAR的详解
一.Spring 常用包的说明 spring.jar : 包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and ...
最新文章
- LeetCode 438. Find All Anagrams in a String--字符串-滑动窗口--C++,Python解法
- Matplotlib实例教程 | markers表
- 006 kkrunchy_Ryd之类FSG压缩壳
- 基于webpack的react脚手架
- JavaScript Document对象DOM
- PHP 7天前的时间戳
- Linux用scp实现无密码传输文件和目录(使用密钥)
- c语言除法中的小数点,高精度除法小数点位数
- 树莓派安装Ubuntu系统
- Spring Boot中使用JUnit5进行单元测试
- 李嘉诚80个人生经典语录
- jvm-虚拟机栈笔记【详细】
- java 定时取出一段时间,Java 实现定时任务的三种方法
- 【专题3:电子工程师 之 上位机】 之 【47.使用QT Opengl显示YUV图像】
- python类计算器开发_开发一个简单的python计算器
- 怎么恢复移动硬盘数据
- Unity 自定义Image形状
- 神经网络—卷积神经网络CNN
- Pycharm安装与入门
- 关于ARM Kinetis MCU唤醒源NVIC和AWIC的工作过程解惑