接口 Environment
- 所有超级接口:
PropertyResolver
- 所有已知实现类:
AbstractEnvironment,MockEnvironment,StandardEnvironment,StandardPortletEnvironment,StandardServletEnvironment
public interface Environment extends PropertyResolver
Interface representing the environment in which the current application is running. Models two key aspects of the application environment: profiles and properties. Methods related to property access are exposed via thePropertyResolversuperinterface.A profile is a named, logical group of bean definitions to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema or the
@Profileannotation for syntax details. The role of theEnvironmentobject with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.Properties 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.
Beans managed within an
ApplicationContextmay register to beEnvironmentAwareor@InjecttheEnvironmentin order to query profile state or resolve properties directly.In most cases, however, application-level beans should not need to interact with the
Environmentdirectly but instead may have to have${...}property values replaced by a property placeholder configurer such asPropertySourcesPlaceholderConfigurer, which itself isEnvironmentAwareand as of Spring 3.1 is registered by default when using<context:property-placeholder/>.Configuration of the environment object must be done through the
ConfigurableEnvironmentinterface, returned from allAbstractApplicationContextsubclassgetEnvironment()methods. SeeConfigurableEnvironmentJavadoc for usage examples demonstrating manipulation of property sources prior to application contextrefresh().- 从以下版本开始:
- 3.1
- 作者:
- Chris Beams
- 另请参阅:
PropertyResolver,EnvironmentCapable,ConfigurableEnvironment,AbstractEnvironment,StandardEnvironment,EnvironmentAware,ConfigurableApplicationContext.getEnvironment(),ConfigurableApplicationContext.setEnvironment(org.springframework.core.env.ConfigurableEnvironment),AbstractApplicationContext.createEnvironment()
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 booleanacceptsProfiles(String... profiles)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.String[]getActiveProfiles()Return the set of profiles explicitly made active for this environment.String[]getDefaultProfiles()Return the set of profiles to be active by default when no active profiles have been set explicitly.从接口继承的方法 org.springframework.core.env.PropertyResolver
containsProperty, getProperty, getProperty, getProperty, getProperty, getPropertyAsClass, getRequiredProperty, getRequiredProperty, resolvePlaceholders, resolveRequiredPlaceholders
方法详细资料
getActiveProfiles
String[] getActiveProfiles()
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 "spring.profiles.active" as a system property or by callingConfigurableEnvironment.setActiveProfiles(String...).If no profiles have explicitly been specified as active, then any default profiles will automatically be activated.
getDefaultProfiles
String[] getDefaultProfiles()
Return the set of profiles to be active by default when no active profiles have been set explicitly.
acceptsProfiles
boolean acceptsProfiles(String... profiles)
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 true if the given profile is not active. For example,env.acceptsProfiles("p1", "!p2")will returntrueif profile 'p1' is active or 'p2' is not active.- 抛出:
IllegalArgumentException- if called with zero arguments or if any profile isnull, empty or whitespace-only- 另请参阅:
getActiveProfiles(),getDefaultProfiles()