Micronaut Mastery: Configuration of a Kotlin based Micronaut application
Micronaut makes it possible to easily configure your application using the provided mechanisms based on Spring and Grails. This blog demonstrates how to configure a Kotlin based Micronaut application using Micronaut version 1.0.0.RC1.
Creat a sample application
First we create a Micronaut application featuring Kotlin:
mn create-app com-jdriven-micronaut-config --features=kotlin
After that we create our DemoController:
cd com-jdriven-micronaut-config
mn create-controller com.jdriven.micronaut.config.DemoController
Injecting plain configuration properties
The controller will render a message using three configuration properties from our application.yml
file.
Change the contents of the application.yml
file in src/main/resources
to:
micronaut:
application:
name: com-jdriven-micronaut-config
server:
port: 8080
demo:
name: Micronaut Config Demo
version: 1.3.1
other-property: some value
Now let’s inject the values in our DemoController
. For illustration purposes we use both constructor and property injection using the @Value
and the @Property
annotation.
package com.jdriven.micronaut.config
import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Value
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
@Controller("/demo")
class DemoController(
@Value("\${demo.name}") val name: String,
@Property(name = "demo.version") val version: String) {
@Value("\${demo.other-property}")
lateinit var otherProperty: String
@Get("/")
fun index(): String {
return "$name, $version, $otherProperty"
}
}
-
Note: Because of the String Template support in Kotlin we have to escape the $ sign if we use the
@Value
annotation. -
As Graeme suggests from version 1.0.0.RC1 we can also use the
@Property
annotation if we don’t want to escape the $ sign. -
Note: Application configuration properties in Micronaut should use Kebab case both in the
application.yml
file and within the@Value
annotation.
Start the Micronaut application and use httpie to verify that our DemoController
works as expected:
http http://localhost:8080/demo
HTTP/1.1 200 OK
Date: Mon, 1 Oct 2018 20:07:26 GMT
connection: keep-alive
content-length: 38
content-type: application/json
Micronaut Config Demo, 1.3.1, some value
Injecting a ConfigurationProperties class
Like Spring Boot and Grails, we can create classes that contain our configuration.
This will make our code more readable and our configuration type safe. The first step is to create a class that contains our configuration and add the ConfigurationProperties
annotation referring to the relevant part of the configuration:
package com.jdriven.micronaut.config
import io.micronaut.context.annotation.ConfigurationProperties
import javax.validation.constraints.NotBlank
@ConfigurationProperties("demo")
class DemoConfig {
@NotBlank
lateinit var name: String
@NotBlank
lateinit var version: String
lateinit var otherProperty: String
}
-
Note: We can add javax.validation annotations to validate our configuration.
-
Note: It’s not possible to use a Kotlin data class as a ConfigurationProperties class.
After that we modify the DemoController
to use the application configuration from the injected DemoConfig
:
package com.jdriven.micronaut.config
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
@Controller("/demo")
class DemoController(val config: DemoConfig) {
@Get("/")
fun index(): String {
return "${config.name}, ${config.version}, ${config.otherProperty}"
}
}
Restart the Micronaut application and use the same httpie command as above to verify that the configuration properties are available.