4.6 Using @EachProperty to Drive Configuration

The @ConfigurationProperties annotation is great for a single configuration class, but sometimes you want multiple instances, each with its own distinct configuration. That is where EachProperty comes in.

The @EachProperty annotation creates a ConfigurationProperties bean for each sub-property within the given property. As an example consider the following class:

Using @EachProperty

  1. import java.net.URI;
  2. import java.net.URISyntaxException;
  3. import io.micronaut.context.annotation.Parameter;
  4. import io.micronaut.context.annotation.EachProperty;
  5. @EachProperty("test.datasource") (1)
  6. public class DataSourceConfiguration {
  7. private final String name;
  8. private URI url = new URI("localhost");
  9. public DataSourceConfiguration(@Parameter String name) (2)
  10. throws URISyntaxException {
  11. this.name = name;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public URI getUrl() { (3)
  17. return url;
  18. }
  19. public void setUrl(URI url) {
  20. this.url = url;
  21. }
  22. }

Using @EachProperty

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

Using @EachProperty

  1. import io.micronaut.context.annotation.EachProperty
  2. import io.micronaut.context.annotation.Parameter
  3. import java.net.URI
  4. import java.net.URISyntaxException
  5. @EachProperty("test.datasource") (1)
  6. class DataSourceConfiguration
  7. @Throws(URISyntaxException::class)
  8. constructor(@param:Parameter val name: String) { (2)
  9. var url = URI("localhost") (3)
  10. }
1The @EachProperty annotation defines the property name to be handled.
2The @Parameter annotation can be used to inject the name of the sub-property that defines the name of the bean (which is also the bean qualifier)
3Each property of the bean is bound to configuration.

The above DataSourceConfiguration defines a url property to configure one or more data sources. The URLs themselves can be configured using any of the PropertySource instances evaluated to Micronaut:

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

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

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:

Evaluating Beans Built by @EachProperty

  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

  1. val beansOfType = applicationContext.getBeansOfType(DataSourceConfiguration::class.java)
  2. assertEquals(2, beansOfType.size) (1)
  3. val firstConfig = applicationContext.getBean(
  4. DataSourceConfiguration::class.java,
  5. Qualifiers.byName("one") (2)
  6. )
  7. assertEquals(
  8. URI("jdbc:mysql://localhost/one"),
  9. firstConfig.url
  10. )
1All beans of type DataSourceConfiguration can be retrieved using getBeansOfType
2Individual beans can be retrieved by using the byName qualifier.

List-Based Binding

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 that the class is bound from a list. Simply set the list member on the annotation to true.

@EachProperty List Example

  1. import io.micronaut.context.annotation.EachProperty;
  2. import io.micronaut.context.annotation.Parameter;
  3. import io.micronaut.core.order.Ordered;
  4. import java.time.Duration;
  5. @EachProperty(value = "ratelimits", list = true) (1)
  6. public class RateLimitsConfiguration implements Ordered { (2)
  7. private final Integer index;
  8. private Duration period;
  9. private Integer limit;
  10. RateLimitsConfiguration(@Parameter Integer index) { (3)
  11. this.index = index;
  12. }
  13. @Override
  14. public int getOrder() {
  15. return index;
  16. }
  17. public Duration getPeriod() {
  18. return period;
  19. }
  20. public void setPeriod(Duration period) {
  21. this.period = period;
  22. }
  23. public Integer getLimit() {
  24. return limit;
  25. }
  26. public void setLimit(Integer limit) {
  27. this.limit = limit;
  28. }
  29. }

@EachProperty List Example

  1. import io.micronaut.context.annotation.EachProperty
  2. import io.micronaut.context.annotation.Parameter
  3. import io.micronaut.core.order.Ordered
  4. import java.time.Duration
  5. @EachProperty(value = "ratelimits", list = true) (1)
  6. class RateLimitsConfiguration implements Ordered { (2)
  7. private final Integer index
  8. Duration period
  9. Integer limit
  10. RateLimitsConfiguration(@Parameter Integer index) { (3)
  11. this.index = index
  12. }
  13. @Override
  14. int getOrder() {
  15. index
  16. }
  17. }

@EachProperty List Example

  1. import io.micronaut.context.annotation.EachProperty
  2. import io.micronaut.context.annotation.Parameter
  3. import io.micronaut.core.order.Ordered
  4. import java.time.Duration
  5. @EachProperty(value = "ratelimits", list = true) (1)
  6. class RateLimitsConfiguration
  7. constructor(@param:Parameter private val index: Int) (3)
  8. : Ordered { (2)
  9. var period: Duration? = null
  10. var limit: Int? = null
  11. override fun getOrder(): Int {
  12. return index
  13. }
  14. }
1The list member of the annotation is set to true
2Implement Ordered if order matters when retrieving the beans
3The index is injected into the constructor