Package org.springframework.core.env

# Interface Environment

All Superinterfaces:
  `PropertyResolver`

All Known Subinterfaces:
  `ConfigurableEnvironment`

All Known Implementing Classes:
  `AbstractEnvironment`, `StandardEnvironment`

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 the `PropertyResolver` superinterface.

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 `@Profile` annotation for syntax details. The role of the `Environment` object 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 `ApplicationContext` may register to be `EnvironmentAware` or `@Inject` the `Environment` in order to query profile state or resolve properties directly.

In most cases, however, application-level beans should not need to interact with the `Environment` directly but instead may request to have `${...}` property values replaced by a property placeholder configurer such as `PropertySourcesPlaceholderConfigurer`, which itself is `EnvironmentAware` and registered by default when using `<context:property-placeholder/>`.

Configuration of the `Environment` object must be done through the `ConfigurableEnvironment` interface, returned from all `AbstractApplicationContext` subclass `getEnvironment()` methods. See `ConfigurableEnvironment` Javadoc for usage examples demonstrating manipulation of property sources prior to application context `refresh()`.

Since:
  3.1
Author:
  Chris Beams, Phillip Webb, Sam Brannen
See Also:
  - `PropertyResolver`
  - `EnvironmentCapable`
  - `ConfigurableEnvironment`
  - `AbstractEnvironment`
  - `StandardEnvironment`
  - `org.springframework.context.EnvironmentAware`
  - `org.springframework.context.ConfigurableApplicationContext#getEnvironment`
  - `org.springframework.context.ConfigurableApplicationContext#setEnvironment`
  - `org.springframework.context.support.AbstractApplicationContext#createEnvironment`

## Method Summary

Modifier and Type
Method
Description
`boolean`
`acceptsProfiles(String... profiles)`
Deprecated.
as of 5.1 in favor of `acceptsProfiles(Profiles)` or `matchesProfiles(String...)`
`boolean`
`acceptsProfiles(Profiles profiles)`
Determine whether the given `Profiles` predicate matches the active profiles — or in the case of no explicit active profiles, whether the given `Profiles` predicate matches the 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.
`default boolean`
`matchesProfiles(String... profileExpressions)`
Determine whether one of the given profile expressions matches the active profiles — or in the case of no explicit active profiles, whether one of the given profile expressions matches the default profiles.

### Methods inherited from interface org.springframework.core.env.PropertyResolver

`containsProperty, getProperty, getProperty, getProperty, getProperty, getRequiredProperty, getRequiredProperty, resolvePlaceholders, resolveRequiredPlaceholders`

## Method Details

### 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 calling `ConfigurableEnvironment.setActiveProfiles(String...)`.

If no profiles have explicitly been specified as active, then any default profiles will automatically be activated.

See Also:
  - `getDefaultProfiles()`
  - `ConfigurableEnvironment.setActiveProfiles(java.lang.String...)`
  - `AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME`

### getDefaultProfiles

String[] getDefaultProfiles()
Return the set of profiles to be active by default when no active profiles have been set explicitly.

See Also:
  - `getActiveProfiles()`
  - `ConfigurableEnvironment.setDefaultProfiles(java.lang.String...)`
  - `AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME`

### matchesProfiles

default boolean matchesProfiles(String... profileExpressions)
Determine whether one of the given profile expressions matches the active profiles — or in the case of no explicit active profiles, whether one of the given profile expressions matches the default profiles.

Profile expressions allow for complex, boolean profile logic to be expressed — for example `"p1 & p2"`, `"(p1 & p2) | p3"`, etc. See `Profiles.of(String...)` for details on the supported expression syntax.

This method is a convenient shortcut for `env.acceptsProfiles(Profiles.of(profileExpressions))`.

Since:
  5.3.28
See Also:
  - `Profiles.of(String...)`
  - `acceptsProfiles(Profiles)`

### acceptsProfiles

@Deprecated boolean acceptsProfiles(String... profiles)
Deprecated.
as of 5.1 in favor of `acceptsProfiles(Profiles)` or `matchesProfiles(String...)`
Determine 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, meaning this method will return `true` if the given profile is not active. For example, `env.acceptsProfiles("p1", "!p2")` will return `true` if profile 'p1' is active or 'p2' is not active.

Throws:
  `IllegalArgumentException` - if called with a `null` array, an empty array, zero arguments or if any profile is `null`, empty, or whitespace only
See Also:
  - `getActiveProfiles()`
  - `getDefaultProfiles()`
  - `matchesProfiles(String...)`
  - `acceptsProfiles(Profiles)`

### acceptsProfiles

boolean acceptsProfiles(Profiles profiles)
Determine whether the given `Profiles` predicate matches the active profiles — or in the case of no explicit active profiles, whether the given `Profiles` predicate matches the default profiles.

If you wish provide profile expressions directly as strings, use `matchesProfiles(String...)` instead.

Since:
  5.1
See Also:
  - `matchesProfiles(String...)`
  - `Profiles.of(String...)`