001/* 002 * Copyright 2002-2016 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 * Interface representing the environment in which the current application is running. 021 * Models two key aspects of the application environment: <em>profiles</em> and 022 * <em>properties</em>. Methods related to property access are exposed via the 023 * {@link PropertyResolver} superinterface. 024 * 025 * <p>A <em>profile</em> is a named, logical group of bean definitions to be registered 026 * with the container only if the given profile is <em>active</em>. Beans may be assigned 027 * to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema 028 * or the {@link org.springframework.context.annotation.Profile @Profile} annotation for 029 * syntax details. The role of the {@code Environment} object with relation to profiles is 030 * in determining which profiles (if any) are currently {@linkplain #getActiveProfiles 031 * active}, and which profiles (if any) should be {@linkplain #getDefaultProfiles active 032 * by default}. 033 * 034 * <p><em>Properties</em> play an important role in almost all applications, and may 035 * originate from a variety of sources: properties files, JVM system properties, system 036 * environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, 037 * Maps, and so on. The role of the environment object with relation to properties is to 038 * provide the user with a convenient service interface for configuring property sources 039 * and resolving properties from them. 040 * 041 * <p>Beans managed within an {@code ApplicationContext} may register to be {@link 042 * org.springframework.context.EnvironmentAware EnvironmentAware} or {@code @Inject} the 043 * {@code Environment} in order to query profile state or resolve properties directly. 044 * 045 * <p>In most cases, however, application-level beans should not need to interact with the 046 * {@code Environment} directly but instead may have to have {@code ${...}} property 047 * values replaced by a property placeholder configurer such as 048 * {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer 049 * PropertySourcesPlaceholderConfigurer}, which itself is {@code EnvironmentAware} and 050 * as of Spring 3.1 is registered by default when using 051 * {@code <context:property-placeholder/>}. 052 * 053 * <p>Configuration of the environment object must be done through the 054 * {@code ConfigurableEnvironment} interface, returned from all 055 * {@code AbstractApplicationContext} subclass {@code getEnvironment()} methods. See 056 * {@link ConfigurableEnvironment} Javadoc for usage examples demonstrating manipulation 057 * of property sources prior to application context {@code refresh()}. 058 * 059 * @author Chris Beams 060 * @since 3.1 061 * @see PropertyResolver 062 * @see EnvironmentCapable 063 * @see ConfigurableEnvironment 064 * @see AbstractEnvironment 065 * @see StandardEnvironment 066 * @see org.springframework.context.EnvironmentAware 067 * @see org.springframework.context.ConfigurableApplicationContext#getEnvironment 068 * @see org.springframework.context.ConfigurableApplicationContext#setEnvironment 069 * @see org.springframework.context.support.AbstractApplicationContext#createEnvironment 070 */ 071public interface Environment extends PropertyResolver { 072 073 /** 074 * Return the set of profiles explicitly made active for this environment. Profiles 075 * are used for creating logical groupings of bean definitions to be registered 076 * conditionally, for example based on deployment environment. Profiles can be 077 * activated by setting {@linkplain AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME 078 * "spring.profiles.active"} as a system property or by calling 079 * {@link ConfigurableEnvironment#setActiveProfiles(String...)}. 080 * <p>If no profiles have explicitly been specified as active, then any 081 * {@linkplain #getDefaultProfiles() default profiles} will automatically be activated. 082 * @see #getDefaultProfiles 083 * @see ConfigurableEnvironment#setActiveProfiles 084 * @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME 085 */ 086 String[] getActiveProfiles(); 087 088 /** 089 * Return the set of profiles to be active by default when no active profiles have 090 * been set explicitly. 091 * @see #getActiveProfiles 092 * @see ConfigurableEnvironment#setDefaultProfiles 093 * @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME 094 */ 095 String[] getDefaultProfiles(); 096 097 /** 098 * Return whether one or more of the given profiles is active or, in the case of no 099 * explicit active profiles, whether one or more of the given profiles is included in 100 * the set of default profiles. If a profile begins with '!' the logic is inverted, 101 * i.e. the method will return true if the given profile is <em>not</em> active. 102 * For example, <pre class="code">env.acceptsProfiles("p1", "!p2")</pre> will 103 * return {@code true} if profile 'p1' is active or 'p2' is not active. 104 * @throws IllegalArgumentException if called with zero arguments 105 * or if any profile is {@code null}, empty or whitespace-only 106 * @see #getActiveProfiles 107 * @see #getDefaultProfiles 108 */ 109 boolean acceptsProfiles(String... profiles); 110 111}