The annotation will create a bean for each sub-property within the given property. As an example consider the following class:

    Using @EachProperty

    Using @EachProperty

    1. @EachProperty("test.datasource")
    2. (1)
    3. class DataSourceConfiguration {
    4. final String name
    5. URI url = new URI("localhost")
    6. DataSourceConfiguration(@Parameter String name) (2)
    7. throws URISyntaxException {
    8. this.name = name
    9. }
    10. }

    Using @EachProperty

    1. @EachProperty("test.datasource") (1)
    2. class DataSourceConfiguration (2)
    3. @Throws(URISyntaxException::class)
    4. constructor(@param:Parameter val name: String) {
    5. (3)
    6. var url = URI("localhost")
    7. }

    Providing Configuration to @EachProperty

    1. ApplicationContext applicationContext = ApplicationContext.run(PropertySource.of(
    2. "test",
    3. CollectionUtils.mapOf(
    4. "test.datasource.one.url", "jdbc:mysql://localhost/one",
    5. "test.datasource.two.url", "jdbc:mysql://localhost/two")
    6. ));

    Providing Configuration to @EachProperty

    Providing Configuration to @EachProperty

    1. val applicationContext = ApplicationContext.run(PropertySource.of(
    2. "test",
    3. mapOf(
    4. "test.datasource.one.url" to "jdbc:mysql://localhost/one",
    5. "test.datasource.two.url" to "jdbc:mysql://localhost/two"
    6. )
    7. ))

    In the above example two data sources (called one and two) are defined under the test.datasource prefix defined earlier in the @EachProperty annotation. Each of these configuration entries triggers the creation of a new DataSourceConfiguration bean such that the following test succeeds:

    1. Collection<DataSourceConfiguration> beansOfType = applicationContext.getBeansOfType(DataSourceConfiguration.class);
    2. assertEquals(2, beansOfType.size()); (1)
    3. DataSourceConfiguration firstConfig = applicationContext.getBean(
    4. DataSourceConfiguration.class,
    5. Qualifiers.byName("one") (2)
    6. );
    7. assertEquals(
    8. new URI("jdbc:mysql://localhost/one"),
    9. firstConfig.getUrl()
    10. );

    Evaluating Beans Built by @EachProperty

    1. when:
    2. Collection<DataSourceConfiguration> beansOfType = applicationContext.getBeansOfType(DataSourceConfiguration.class)
    3. assertEquals(2, beansOfType.size()) (1)
    4. DataSourceConfiguration firstConfig = applicationContext.getBean(
    5. DataSourceConfiguration.class,
    6. Qualifiers.byName("one") (2)
    7. )
    8. then:
    9. new URI("jdbc:mysql://localhost/one") == firstConfig.getUrl()

    Evaluating Beans Built by @EachProperty

    The default behavior of @EachProperty is to bind from a map style of configuration where the key is the named qualifier of the bean and the value is the data to bind from. For cases where map style configuration doesn’t make sense, it is possible to inform Micronaut the class should be bound from a list. Simply set the list member on the annotation to true.

    @EachProperty List Example

    1. /*
    2. * Copyright 2017-2020 original authors
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * You may obtain a copy of the License at
    6. *
    7. * https://www.apache.org/licenses/LICENSE-2.0
    8. *
    9. * Unless required by applicable law or agreed to in writing, software
    10. * distributed under the License is distributed on an "AS IS" BASIS,
    11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. * See the License for the specific language governing permissions and
    13. * limitations under the License.
    14. */
    15. package io.micronaut.docs.config.env;
    16. import io.micronaut.context.annotation.EachProperty;
    17. import io.micronaut.context.annotation.Parameter;
    18. import io.micronaut.core.order.Ordered;
    19. import java.time.Duration;
    20. @EachProperty(value = "ratelimits", list = true) (1)
    21. public class RateLimitsConfiguration implements Ordered { (2)
    22. private final Integer index;
    23. private Duration period;
    24. private Integer limit;
    25. RateLimitsConfiguration(@Parameter Integer index) { (3)
    26. this.index = index;
    27. }
    28. @Override
    29. public int getOrder() {
    30. return index;
    31. }
    32. public Duration getPeriod() {
    33. return period;
    34. }
    35. public void setPeriod(Duration period) {
    36. this.period = period;
    37. }
    38. public Integer getLimit() {
    39. return limit;
    40. }
    41. public void setLimit(Integer limit) {
    42. this.limit = limit;
    43. }
    44. }
    1. /*
    2. * Copyright 2017-2020 original authors
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. * Unless required by applicable law or agreed to in writing, software
    10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11. * See the License for the specific language governing permissions and
    12. * limitations under the License.
    13. */
    14. package io.micronaut.docs.config.env
    15. import io.micronaut.context.annotation.EachProperty
    16. import io.micronaut.context.annotation.Parameter
    17. import io.micronaut.core.order.Ordered
    18. import java.time.Duration
    19. @EachProperty(value = "ratelimits", list = true) (1)
    20. class RateLimitsConfiguration implements Ordered { (2)
    21. private final Integer index
    22. Duration period
    23. Integer limit
    24. RateLimitsConfiguration(@Parameter Integer index) { (3)
    25. this.index = index
    26. }
    27. @Override
    28. int getOrder() {
    29. index
    30. }
    31. }

    @EachProperty List Example

    1. /*
    2. * Copyright 2017-2020 original authors
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package io.micronaut.docs.config.env
    17. import io.micronaut.context.annotation.EachProperty
    18. import io.micronaut.context.annotation.Parameter
    19. import io.micronaut.core.order.Ordered
    20. import java.time.Duration
    21. @EachProperty(value = "ratelimits", list = true) (1)
    22. class RateLimitsConfiguration
    23. constructor(@param:Parameter private val index: Int) (3)
    24. : Ordered { (2)
    25. var period: Duration? = null
    26. var limit: Int? = null
    27. override fun getOrder(): Int {
    28. return index
    29. }
    30. }