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 Details

  • Method Details

    • initialize

      public void initialize(@NonNull org.springframework.context.ConfigurableApplicationContext applicationContext)
      Initializes the Spring application context by loading properties from the available .env file using EnvContextLoader.

      This method instantiates an EnvContextLoader to handle the reading of the .env file. It calls EnvContextLoader.load() to load the properties from the .env file. The loaded properties are then added to the Spring application context's environment.

      Specified by:
      initialize in interface org.springframework.context.ApplicationContextInitializer<org.springframework.context.ConfigurableApplicationContext>
      Parameters:
      applicationContext - the ConfigurableApplicationContext to initialize
      See Also: