For this purpose, the annotation allows you to bind a map to a single configuration property, and specify whether to accept a flat map of keys to values, or a nested map (where the values may be additional maps].

    @MapFormat Example

    @MapFormat Example

    1. import javax.validation.constraints.Min
    2. import io.micronaut.core.convert.format.MapFormat
    3. @ConfigurationProperties('my.engine')
    4. class EngineConfig {
    5. @Min(1L)
    6. int cylinders
    7. @MapFormat(transformation = MapFormat.MapTransformation.FLAT) (1)
    8. Map<Integer, String> sensors
    9. }
    1. import io.micronaut.context.annotation.ConfigurationProperties
    2. import javax.validation.constraints.Min
    3. import io.micronaut.core.convert.format.MapFormat
    4. @ConfigurationProperties("my.engine")
    5. class EngineConfig {
    6. @Min(1L)
    7. var cylinders: Int = 0
    8. var sensors: Map<Int, String>? = null

    EngineImpl

    EngineImpl

    1. @Singleton
    2. class EngineImpl implements Engine {
    3. @Inject EngineConfig config
    4. @Override
    5. Map getSensors() {
    6. config.sensors
    7. }
    8. String start() {
    9. "Engine Starting V${config.cylinders} [sensors=${sensors.size()}]"
    10. }
    11. }

    EngineImpl

    1. @Singleton
    2. class EngineImpl : Engine {
    3. override val sensors: Map<*, *>?
    4. get() = config!!.sensors
    5. @Inject
    6. var config: EngineConfig? = null
    7. override fun start(): String {
    8. return "Engine Starting V${config!!.cylinders} [sensors=${sensors!!.size}]"
    9. }
    10. }

    Use Map Configuration

    Use Map Configuration

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

    Use Map Configuration

    1. val subMap = mapOf(
    2. 0 to "thermostat",
    3. 1 to "fuel pressure"
    4. )
    5. val map = mapOf(
    6. "my.engine.cylinders" to "8",
    7. "my.engine.sensors" to subMap
    8. )
    9. val applicationContext = ApplicationContext.run(map, "test")
    10. val vehicle = applicationContext.getBean(Vehicle::class.java)