Class EnvContextLoader

java.lang.Object
io.sysr.springcontext.env.EnvContextLoader

public class EnvContextLoader extends Object
The EnvContextLoader class is responsible for loading environment variables from .env files or an env.properties file into a properties map. It handles variable resolution, including nested variables, validates variable names and detects circular dependencies.

Example usage:


 // Create an instance of the loader
 EnvContextLoader loader = new EnvContextLoader();

 // Load environment variables
 loader.load();

 // Retrieve the loaded properties
 Properties properties = loader.getLoadedProperties();

 // Access a property
 String dbUrl = properties.getProperty("DATABASE_URL");
 

Note: Ensure that the dotenv properties configuration file (dotenv.properties) is placed in the resources foler. This only applies if you have the .env file that contains the environment declarions is placed in a custom location.

Since:
2024
  • Constructor Details

    • EnvContextLoader

      public EnvContextLoader()
      Constructs a new EnvContextLoader instance.
  • Method Details

    • getLoadedProperties

      public Properties getLoadedProperties()
      Retrieves the loaded properties as a Properties object.
      Returns:
      The Properties object containing all loaded and resolved environment variables.
    • load

      public void load()
      Initiates the loading process of environment variables.

      It attempts to load variables from a user-provided file which is specified in the dotenv.properties file or from .env files in the default root directory where JVM is invoked.

      Throws:
      EnvContextLoaderException - if there is any error during the loading process.