In modern cloud-native applications, externalizing configuration is key. Micronaut provides a powerful and easy-to-use mechanism for injecting custom properties and managing configuration.
In this guide, you will learn how to:
- Create custom configuration classes
- Inject properties into beans and services
- Use application configuration files effectively
We’ll use Maven, Java, and the package com.kscodes.micronaut.advanced
for demonstration.

๐ฆ Maven Setup
Ensure you have the following dependencies in your pom.xml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<dependency> <groupId>io.micronaut</groupId> <artifactId>micronaut-inject</artifactId> </dependency> <dependency> <groupId>io.micronaut</groupId> <artifactId>micronaut-runtime</artifactId> </dependency> <dependency> <groupId>io.micronaut</groupId> <artifactId>micronaut-validation</artifactId> </dependency> |
๐ ๏ธ Step 1: Create Custom Configuration Class
Micronaut uses the @ConfigurationProperties
annotation to bind external config properties to a POJO.
๐ธ com.kscodes.micronaut.advanced.config.AppSettings.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kscodes.micronaut.advanced.config; import io.micronaut.context.annotation.ConfigurationProperties; import jakarta.validation.constraints.NotBlank; @ConfigurationProperties("app.settings") public class AppSettings { @NotBlank private String appName; private int maxUsers; public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public int getMaxUsers() { return maxUsers; } public void setMaxUsers(int maxUsers) { this.maxUsers = maxUsers; } } |
๐ Step 2: Define Properties in application.yml
1 2 3 4 5 6 7 |
app: settings: app-name: "KSCodes Micronaut App" max-users: 50 |
Micronaut will automatically bind these values to the AppSettings
bean.
๐งฉ Step 3: Inject Configuration in a Service
You can inject the configuration bean using @Inject
.
๐ธ com.kscodes.micronaut.advanced.service.AppService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kscodes.micronaut.advanced.service; import com.kscodes.micronaut.advanced.config.AppSettings; import jakarta.inject.Singleton; @Singleton public class AppService { private final AppSettings appSettings; public AppService(AppSettings appSettings) { this.appSettings = appSettings; } public void printSettings() { System.out.println("App Name: " + appSettings.getAppName()); System.out.println("Max Users: " + appSettings.getMaxUsers()); } } |
๐งช Step 4: Run and Test
Create a main class to run and validate the injection.
๐ธ com.kscodes.micronaut.advanced.Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.kscodes.micronaut.advanced; import com.kscodes.micronaut.advanced.service.AppService; import io.micronaut.runtime.Micronaut; import jakarta.inject.Inject; public class Application { @Inject static AppService appService; public static void main(String[] args) { Micronaut.run(Application.class); appService.printSettings(); } } |
Note: For testing, consider using
@MicronautTest
and@Property
annotations to override values.
๐ Supporting Multiple Environments
You can create environment-specific files like application-dev.yml
, application-prod.yml
:
1 2 3 4 5 6 7 8 |
# application-dev.yml app: settings: app-name: "Dev App" max-users: 10 |
Activate an environment using:
1 2 3 4 |
java -Dmicronaut.environments=dev -jar yourapp.jar |
๐ Bonus: Injecting Values Without a POJO
Micronaut also allows property injection using @Value
:
1 2 3 4 5 |
@Value("${app.settings.app-name}") String appName; |
This is useful for simple values, but for structured configuration, prefer @ConfigurationProperties
.
โ Conclusion
Custom configuration and property injection in Micronaut make your applications clean, modular, and flexible for any environment.
Youโve learned:
- How to bind properties with
@ConfigurationProperties
- How to inject configuration into services
- How to manage profiles and environments
This approach aligns well with 12-factor app methodology and prepares your app for scalable deployment.