Micronaut will produce a reflection-free bean and will also at compile time calculate the property paths to evaluate, greatly improving the speed and efficiency of loading @ConfigurationProperties.

    An example of a configuration class can be seen below:

    @ConfigurationProperties Example

    @ConfigurationProperties Example

    1. import io.micronaut.context.annotation.ConfigurationProperties
    2. import javax.validation.constraints.Min
    3. import javax.validation.constraints.NotBlank
    4. @ConfigurationProperties('my.engine') (1)
    5. class EngineConfig {
    6. @NotBlank (2)
    7. String manufacturer = "Ford" (3)
    8. @Min(1L)
    9. int cylinders
    10. CrankShaft crankShaft = new CrankShaft()
    11. @ConfigurationProperties('crank-shaft')
    12. static class CrankShaft { (4)
    13. Optional<Double> rodLength = Optional.empty() (5)
    14. }
    15. }
    1. import io.micronaut.context.annotation.ConfigurationProperties
    2. import java.util.*
    3. import javax.validation.constraints.Min
    4. import javax.validation.constraints.NotBlank
    5. @ConfigurationProperties("my.engine") (1)
    6. @NotBlank (2)
    7. var manufacturer = "Ford" (3)
    8. @Min(1L)
    9. var cylinders: Int = 0
    10. var crankShaft = CrankShaft()
    11. @ConfigurationProperties("crank-shaft")
    12. class CrankShaft { (4)
    13. var rodLength: Optional<Double> = Optional.empty() (5)
    14. }
    15. }

    Once you have prepared a type safe configuration it can simply be injected into your objects like any other bean:

    @ConfigurationProperties Dependency Injection

    @ConfigurationProperties Dependency Injection

    1. @Singleton
    2. class EngineImpl implements Engine {
    3. final EngineConfig config
    4. EngineImpl(EngineConfig config) { (1)
    5. this.config = config
    6. }
    7. @Override
    8. int getCylinders() {
    9. config.cylinders
    10. }
    11. String start() { (2)
    12. }
    13. }

    @ConfigurationProperties Dependency Injection

    1. @Singleton
    2. class EngineImpl(val config: EngineConfig) : Engine {(1)
    3. override val cylinders: Int
    4. get() = config.cylinders
    5. override fun start(): String {(2)
    6. return "${config.manufacturer} Engine Starting V${config.cylinders} [rodLength=${config.crankShaft.rodLength.orElse(6.0)}]"
    7. }
    8. }
    1Inject the EngineConfig bean
    2Use the configuration properties

    Supply Configuration

    Supply Configuration

    1. ApplicationContext applicationContext = ApplicationContext.run(
    2. ['my.engine.cylinders': '8'],
    3. "test"
    4. )
    5. Vehicle vehicle = applicationContext
    6. .getBean(Vehicle)
    7. println(vehicle.start())

    Supply Configuration

    1. val map = mapOf( "my.engine.cylinders" to "8")
    2. val applicationContext = ApplicationContext.run(map, "test")
    3. val vehicle = applicationContext.getBean(Vehicle::class.java)

    The above example prints: "Ford Engine Starting V8 [rodLength=6.0]"

    For example creating a subclass of EngineConfig with @ConfigurationProperties('bar') will resolve all properties under the path .