The Environment abstraction in Spring is an interface, available from every ApplicationContext, that unifies two things: property resolution (reading configuration values from many sources) and profile management (activating different bean sets and settings per deployment, such as dev, test or prod). You use it to read values like app.name, to check which profiles are active, and to add property sources programmatically.
In a Spring Boot application, the Environment is already filled for you from application.properties or application.yml, environment variables, system properties and command-line arguments.
Environment extends PropertyResolver, so it offers getProperty, getRequiredProperty and containsProperty. Behind it sits a MutablePropertySources list of PropertySource objects, searched in order. The first source that contains a key wins, which is how overriding works.
The concrete type in most applications is ConfigurableEnvironment, which lets you modify property sources and profiles before the context refreshes.
Typical sources in Spring Boot, from highest to lowest precedence, include command-line arguments, SPRING_APPLICATION_JSON, OS environment variables, application-{profile} files, and then application files. The full order is defined in the Spring Boot reference documentation; check it for your version before relying on a subtle override.
Inject Environment and call getProperty.
getProperty(key, default) returns the default when the key is absent, getProperty(key, Class, default) converts the value, and getRequiredProperty throws IllegalStateException when the key is missing.
| Approach | Best for |
|---|---|
Environment | Dynamic key lookups, checking whether a key exists, library or framework code |
@Value("${app.name}") | One or two simple values in a bean |
@ConfigurationProperties | Grouped, typed, validated configuration; the preferred option in Spring Boot |
For normal application settings, prefer @ConfigurationProperties with a record or class, and use Environment when the key is only known at runtime.
Profiles let you register beans or load properties only for certain environments. The Environment tells you which are active.
getActiveProfiles() returns the explicitly activated profiles, and getDefaultProfiles() returns the fallback (default) used when none is active. Use matchesProfiles(...) with a profile expression; the older acceptsProfiles(String...) is deprecated in favour of acceptsProfiles(Profiles).
Activate profiles outside the code:
Profile-specific files such as application-prod.yml are loaded on top of application.yml. Inside a single multi-document YAML file, use spring.config.activate.on-profile (this replaced spring.profiles in Spring Boot 2.4). Profile groups, set with spring.profiles.group.prod, activate several profiles together.
Beans are tied to profiles with @Profile:
@PropertySource adds a file to the Environment. For Boot applications, spring.config.import is the more flexible option. You can also add sources in code before refresh through ConfigurableEnvironment:
addFirst gives the new source the highest priority. Do this early, for example in an EnvironmentPostProcessor or ApplicationContextInitializer, because beans read properties during startup.
Besides constructor injection, a bean can implement EnvironmentAware and receive the Environment through setEnvironment. Configuration classes can also take it as a parameter of a @Bean method. Keep secrets out of application.properties in source control; supply them through environment variables or a secrets manager and read them via the same abstraction.
ApplicationContext is the container that manages beans. The Environment is a part of it that handles properties and profiles. You get it with applicationContext.getEnvironment().
Inject Environment and call getActiveProfiles(). If no profile is set, the array is empty and getDefaultProfiles() returns default.
Environment variable names must use the relaxed form, such as APP_CUSTOM_NAME for app.custom.name, and a higher-precedence source may also define the key. Check the property source list in the /actuator/env endpoint if it is enabled.
Use @ConfigurationProperties for structured settings, @Value for a single value, and Environment when you need dynamic lookups or to inspect profiles.
The Environment abstraction gives Spring one API for reading properties from many ordered sources and for working with profiles. Inject it for dynamic lookups, prefer @ConfigurationProperties for typed settings, activate profiles with spring.profiles.active, and use @Profile to switch beans per environment.