Class SpringContextEnvApplicationContextInitializer
java.lang.Object
io.sysr.springcontext.env.configuration.SpringContextEnvApplicationContextInitializer
- All Implemented Interfaces:
org.springframework.context.ApplicationContextInitializer<org.springframework.context.ConfigurableApplicationContext>
@Order(-2147483648)
public class SpringContextEnvApplicationContextInitializer
extends Object
implements org.springframework.context.ApplicationContextInitializer<org.springframework.context.ConfigurableApplicationContext>
A Spring
ApplicationContextInitializer that loads environment
variables from .env files into the Spring application
context's environment. This initializer enhances Spring's configuration
by injecting the contents of .env files as additional
properties.
It utilizes EnvContextLoader to read and load any provided
.env (dotenv) files.
This class ensures that when a Spring application starts, it can
automatically load environment variables from any existing
.env file(s), making them available through Spring's property
resolution system.
The properties loaded from the .env file are added to the
Spring application context's environment, allowing for seamless integration
of environment-specific configurations.
- Since:
- 2024
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for the class. -
Method Summary
Modifier and TypeMethodDescriptionvoidinitialize(org.springframework.context.ConfigurableApplicationContext applicationContext) Initializes the Spring application context by loading properties from the available.envfile usingEnvContextLoader.
-
Constructor Details
-
SpringContextEnvApplicationContextInitializer
public SpringContextEnvApplicationContextInitializer()Default constructor for the class. Initializes a new instance ofSpringContextEnvApplicationContextInitializer.
-
-
Method Details
-
initialize
public void initialize(@NonNull org.springframework.context.ConfigurableApplicationContext applicationContext) Initializes the Spring application context by loading properties from the available.envfile usingEnvContextLoader.This method instantiates an
EnvContextLoaderto handle the reading of the.envfile. It callsEnvContextLoader.load()to load the properties from the.envfile. The loaded properties are then added to the Spring application context's environment.- Specified by:
initializein interfaceorg.springframework.context.ApplicationContextInitializer<org.springframework.context.ConfigurableApplicationContext>- Parameters:
applicationContext- theConfigurableApplicationContextto initialize- See Also:
-