001/* 002 * Copyright 2002-2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.env; 018 019/** 020 * {@link Environment} implementation suitable for use in 'standard' (i.e. non-web) 021 * applications. 022 * 023 * <p>In addition to the usual functions of a {@link ConfigurableEnvironment} such as 024 * property resolution and profile-related operations, this implementation configures two 025 * default property sources, to be searched in the following order: 026 * <ul> 027 * <li>{@linkplain AbstractEnvironment#getSystemProperties() system properties} 028 * <li>{@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables} 029 * </ul> 030 * 031 * That is, if the key "xyz" is present both in the JVM system properties as well as in 032 * the set of environment variables for the current process, the value of key "xyz" from 033 * system properties will return from a call to {@code environment.getProperty("xyz")}. 034 * This ordering is chosen by default because system properties are per-JVM, while 035 * environment variables may be the same across many JVMs on a given system. Giving 036 * system properties precedence allows for overriding of environment variables on a 037 * per-JVM basis. 038 * 039 * <p>These default property sources may be removed, reordered, or replaced; and 040 * additional property sources may be added using the {@link MutablePropertySources} 041 * instance available from {@link #getPropertySources()}. See 042 * {@link ConfigurableEnvironment} Javadoc for usage examples. 043 * 044 * <p>See {@link SystemEnvironmentPropertySource} javadoc for details on special handling 045 * of property names in shell environments (e.g. Bash) that disallow period characters in 046 * variable names. 047 * 048 * @author Chris Beams 049 * @since 3.1 050 * @see ConfigurableEnvironment 051 * @see SystemEnvironmentPropertySource 052 * @see org.springframework.web.context.support.StandardServletEnvironment 053 */ 054public class StandardEnvironment extends AbstractEnvironment { 055 056 /** System environment property source name: {@value} */ 057 public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME = "systemEnvironment"; 058 059 /** JVM system properties property source name: {@value} */ 060 public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties"; 061 062 063 /** 064 * Customize the set of property sources with those appropriate for any standard 065 * Java environment: 066 * <ul> 067 * <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} 068 * <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME} 069 * </ul> 070 * <p>Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will 071 * take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}. 072 * @see AbstractEnvironment#customizePropertySources(MutablePropertySources) 073 * @see #getSystemProperties() 074 * @see #getSystemEnvironment() 075 */ 076 @Override 077 protected void customizePropertySources(MutablePropertySources propertySources) { 078 propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties())); 079 propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); 080 } 081 082}