4.1 The Environment

The application environment is modelled by the Environment interface, which allows specifying one or many unique environment names when creating an ApplicationContext.

Initializing the Environment

  1. ApplicationContext applicationContext = ApplicationContext.run("test", "android");
  2. Environment environment = applicationContext.getEnvironment();
  3. assertTrue(environment.getActiveNames().contains("test"));
  4. assertTrue(environment.getActiveNames().contains("android"));

Initializing the Environment

  1. when:
  2. ApplicationContext applicationContext = ApplicationContext.run("test", "android")
  3. Environment environment = applicationContext.getEnvironment()
  4. then:
  5. environment.getActiveNames().contains("test")
  6. environment.getActiveNames().contains("android")

Initializing the Environment

  1. val applicationContext = ApplicationContext.run("test", "android")
  2. val environment = applicationContext.getEnvironment()
  3. assertTrue(environment.getActiveNames().contains("test"))
  4. assertTrue(environment.getActiveNames().contains("android"))

The active environment names serve the purpose of allowing loading different configuration files depending on the environment and also using the @Requires annotation to conditionally load beans or bean @Configuration packages.

In addition, Micronaut will attempt to detect the current environments. For example within a Spock or JUnit test the TEST environment will be automatically active.

Additional active environments can be specified using the micronaut.environments system property or the MICRONAUT_ENVIRONMENTS environment variable. These can be specified as a comma separated list. For example:

Specifying environments

  1. $ java -Dmicronaut.environments=foo,bar -jar myapp.jar

The above activates environments called foo and bar.

Finally, the Cloud environment names are also detected. See the section on Cloud Configuration for more information.