Reputation: 87
I'm new to micronaut and server side programming in general. The micronaut documentation, unfortunately, does not make a lot of sense to me, as I do not have a Java background. A lot of the terms like "ApplicationContext" make sense in english, but I have no idea how to use them in practice.
Trying to start with a very basic app that prints different configurations ("localhost", "dev", "prod") depending on the environment it is in.
Here's my controller
@Controller("/")
class EnvironmentController {
// this should return "localhost", "DEV", "PROD" depending on the environment
@Get("/env")
@Produces(MediaType.TEXT_PLAIN)
fun env() = "???" // what should I put here ?
// this should return the correct mongodb connection string for the environment
@Get("/mongo")
@Produces(MediaType.TEXT_PLAIN)
fun mongo() = "???" // what should I put here ?
}
Here's the application.yml
. Ideally I'd have 1 yml file for each environment
micronaut:
application:
name: myApp
server:
port: 8090
environment: localhost
mongodb:
uri: 'mongodb://localhost:27017'
Application.kt
is untouched with the rest of the files generated by the mn
cli tool. How can I set per environment parameters, or pass the yml file as a parameter when starting micronaut?
Are there any conventions around this?
Upvotes: 5
Views: 25705
Reputation: 11657
To be able to use environment specific variables in controllers and other classes., we have to inject them into fields.
Property values can be loaded with the @Value
, @Property
or @ConfigurationProperties
annotations.
@Property(name='app.message', defaultValue = 'unknown')
private String message
@Property(name='env.test', defaultValue = 'env.test not set')
private String envt
@Property(name='env.dev', defaultValue = 'env.dev not set')
private String envd
Assuming that app.message
is defined in both dev and test environments, it is loaded from the property source that is defined last. (For instance, in MICRONAUT_ENVIRONMENTS=dev,test
test is defined last and takes precedence.)
If env.test
is defined in the test environment, it is loaded from the application-test.properties
property source. Likewise, the env.dev
is loaded from application-dev.properties
.
Also note that the default environment set by the application builder's defaultEnvironments
method is only applied when no other active environments are detected. Otherwise, the default environment is ignored.
Upvotes: 0
Reputation: 17118
By default Micronaut only looks for application.yml. Then, for tests,dev and prod, it loads application.yml and overrides any values there with the ones defined in application-test.yml, application-dev.yml and application-prod.yml If you want to enable any other environment, you need to do it manually
public static void main(String[] args) {
Micronaut.build(args)
.mainClass(Application.class)
.defaultEnvironments("dev")
.start();
}
https://docs.micronaut.io/latest/guide/index.html#_default_environment
Upvotes: 3
Reputation: 12228
You can specify an environment with -Dmicronaut.environments
, or by specifying them in the context builder Micronaut.run
in your Application class.
https://docs.micronaut.io/latest/guide/index.html#environments
Then for example application-env.yml
will be loaded.
https://docs.micronaut.io/latest/guide/index.html#propertySource
The docs are pretty clear on this
Upvotes: 10