@Configuration and @ConfigurationBuilder
Learn how to use @Configuration and @ConfigurationBuilder annotations to configure declared properties.
On this guide
In this section
Getting Started
In this guide, we will create a Micronaut application written in Groovy.
In this guide, you will learn how to use the annotations @ConfigurationProperties, @ConfigurationBuilder, and @EachProperty to consume configured properties in a Micronaut application. These annotations allow declared values to be injected into a bean for use in the application.
What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately
Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
Writing the Application
Create an application using the Micronaut Command Line Interface or with Micronaut Launch.
mn create-app example.micronaut.micronautguide \
--features=serialization-jackson \
--build=maven \
--lang=groovy \
--test=spock|
Note
|
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.
If you use Micronaut Launch, select Micronaut Application as application type and add serialization-jackson features.
|
Note
|
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
Team Configuration with @ConfigurationProperties
Imagine a feature where you can configure a sports team in a declarative manner. The team has a few attributes like team name, color, and players.
team.name=Steelers
team.color=Black
team.player-names[0]=Mason Rudolph
team.player-names[1]=James ConnorWith Micronaut Framework, you can use the @ConfigurationProperties annotation to bind the configuration into a bean. Each property that matches the configuration in application.properties will call the setter in the bean. The bean will then be available for injection in the application.
|
Note
|
Empty tag |
Test @ConfigurationProperties
Let’s validate that the bean is available in the application context and is created with the values declared in the application.properties.
Team Admin Builder with @ConfigurationBuilder
The Builder pattern is a great way to build configuration objects incrementally. Read about the Builder pattern in this
DZone article to learn more. Micronaut Framework supports the Builder pattern with @ConfigurationBuilder.
Suppose you want to add team administrators to a team. The team administration is composed by using a builder pattern object. You can add a coach, manager, and president to the team.
The TeamAdmin object abides by the Builder pattern.
At the bottom of TeamConfiguration, add the inner class TeamAdmin.Builder and annotate it with @ConfigurationBuilder.
This tells Micronaut Framework that configuration can be read and an object can be constructed using the Builder pattern.
|
Note
|
The builder is used only here, so call builder.build() later to get the TeamAdmin object. In this guide, the JUnit test calls builder.build().
|
Test @ConfigurationBuilder
Validate that @ConfigurationBuilder is applied properly with the following JUnit test. The test format is similar to previous tests.
Stadiums with @EachProperty
Micronaut Framework is also able to read a list of related configurations. Imagine you would like to declare stadiums and their attributes.
You can use @EachProperty to cycle through the configuration and read each nested clause as a bean. The higher-level
property will be parameterized as the name.
Test @EachProperty
Validate the configuration with a test. Notice multiple beans are created from the configuration. In a controller, you can
inject a particular StadiumConfiguration instance bean by using the @Named parameter with a qualifier name.
Running the Application
To run the application, use the ./mvnw mn:run command, which starts the application on port 8080.
Controller
Configuration beans can be injected into the application just like any other beans. As a demonstration, create a controller where the beans are constructor injected. The StadiumConfiguration class has two instances, so for injection you need to use the @Named annotation with a qualifier name to specify the bean.
|
Note
|
In the browser go to http://localhost:8080/my/team and http://localhost:8080/my/stadium. |
Add test:
package example.micronaut
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Specification
import jakarta.inject.Inject
@MicronautTest
class MyControllerSpec extends Specification {
@Inject
@Client("/")
HttpClient client
void 'test my team'() {
when:
TeamConfiguration teamConfiguration = client.toBlocking().retrieve(HttpRequest.GET("/my/team"), TeamConfiguration)
then:
teamConfiguration.name == 'Steelers'
teamConfiguration.color == 'Black'
and:
teamConfiguration.playerNames.size() == 2
teamConfiguration.playerNames == ['Mason Rudolph', 'James Connor']
}
}Next Steps
Visit Micronaut Application Configuration to learn more.
Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
License
|
Note
|
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |