Spring
June 7, 2020

M01 Q31 What is the Environment abstraction?

Environment Abstraction is part of the Spring Container that models two key aspects of the application environment:

  • Profiles
  • Properties

Environment Abstraction is represented on code level by classes that implements Environment interface. This interface allows you to resolve properties and also to list profiles. You can receive a reference to the class that implements Environment by calling EnvironmentCapable class, implemented by ApplicationContext.

Properties can also be retrieved by using @Value("${…}") annotation.

Environment Abstraction role in the context of profiles is to determine which profiles are currently active, and which are activated by default.

Environment Abstraction role in the context of properties is to provide convenient, standardized and generic service that allows to resolve properties and also to configure property sources.

Properties may come from the following sources:

  • Properties Files
  • JVM system properties
  • System Environment Variables
  • JNDI
  • Servlet Config
  • Servlet Context Parameters

Default property sources for standalone applications are configured in StandardEnvironment, which includes JVM system properties and System Environment Variables.

When running Spring Application in Servlet Environment, property sources will be configured based on StandardServletEnvironment, which additionally includes Servlet Config and Servlet Context Parameters, optionally it might include JndiPropertySource.

To add additional properties files as property sources you can use @PropertySource annotation.