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
ApplicationContext applicationContext = ApplicationContext.run("test", "android");
Environment environment = applicationContext.getEnvironment();
assertTrue(environment.getActiveNames().contains("test"));
assertTrue(environment.getActiveNames().contains("android"));
Initializing the Environment
when:
ApplicationContext applicationContext = ApplicationContext.run("test", "android")
Environment environment = applicationContext.getEnvironment()
then:
environment.getActiveNames().contains("test")
environment.getActiveNames().contains("android")
Initializing the Environment
val applicationContext = ApplicationContext.run("test", "android")
val environment = applicationContext.getEnvironment()
assertTrue(environment.getActiveNames().contains("test"))
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
$ 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.