diff --git a/context/src/main/java/io/micronaut/runtime/context/env/ConfigurationIntroductionAdvice.java b/context/src/main/java/io/micronaut/runtime/context/env/ConfigurationIntroductionAdvice.java index 3c889493f2e..8789c3bd9b4 100644 --- a/context/src/main/java/io/micronaut/runtime/context/env/ConfigurationIntroductionAdvice.java +++ b/context/src/main/java/io/micronaut/runtime/context/env/ConfigurationIntroductionAdvice.java @@ -30,6 +30,7 @@ import io.micronaut.core.type.Argument; import io.micronaut.core.type.ReturnType; import io.micronaut.core.value.PropertyNotFoundException; +import io.micronaut.inject.qualifiers.Qualifiers; import java.util.Optional; @@ -71,8 +72,10 @@ public Object intercept(MethodInvocationContext context) { final ReturnType rt = context.getReturnType(); final Class returnType = rt.getType(); if (context.isTrue(ConfigurationAdvice.class, MEMBER_BEAN)) { + final Qualifier qualifier = name != null ? Qualifiers.byName(name) : null; + if (context.isNullable()) { - final Object v = beanContext.findBean(returnType).orElse(null); + final Object v = beanContext.findBean(returnType, qualifier).orElse(null); if (v != null) { return environment.convertRequired(v, returnType); } else { @@ -80,7 +83,7 @@ public Object intercept(MethodInvocationContext context) { } } else { return environment.convertRequired( - beanContext.getBean(returnType), + beanContext.getBean(returnType, qualifier), returnType ); } diff --git a/inject-java/src/test/groovy/io/micronaut/inject/foreach/EachPropertyInterfaceSpec.groovy b/inject-java/src/test/groovy/io/micronaut/inject/foreach/EachPropertyInterfaceSpec.groovy index 86809bf5c6b..38e9f7f2bc5 100644 --- a/inject-java/src/test/groovy/io/micronaut/inject/foreach/EachPropertyInterfaceSpec.groovy +++ b/inject-java/src/test/groovy/io/micronaut/inject/foreach/EachPropertyInterfaceSpec.groovy @@ -2,8 +2,6 @@ package io.micronaut.inject.foreach import io.micronaut.context.ApplicationContext import io.micronaut.inject.qualifiers.Qualifiers -import spock.lang.AutoCleanup -import spock.lang.Shared import spock.lang.Specification class EachPropertyInterfaceSpec extends Specification { @@ -30,4 +28,27 @@ class EachPropertyInterfaceSpec extends Specification { cleanup: context.close() } + + void "test @EachProperty on interface with nested @ConfigurationProperties"() { + given: + def context = ApplicationContext.run( + "foo.bar[0].address.host": 'host1', + "foo.bar[0].address.port": '9999', + "foo.bar[1].address.host": 'host2', + "foo.bar[1].address.port": '8888' + ) + + when: + def one = context.getBean(InterfaceAggregatorConfig, Qualifiers.byName("0")) + def two = context.getBean(InterfaceAggregatorConfig, Qualifiers.byName("1")) + + then: + one.address.host == 'host1' + one.address.port == 9999 + two.address.host == 'host2' + two.address.port == 8888 + + cleanup: + context.close() + } } diff --git a/inject-java/src/test/groovy/io/micronaut/inject/foreach/InterfaceAggregatorConfig.java b/inject-java/src/test/groovy/io/micronaut/inject/foreach/InterfaceAggregatorConfig.java new file mode 100644 index 00000000000..631415f272b --- /dev/null +++ b/inject-java/src/test/groovy/io/micronaut/inject/foreach/InterfaceAggregatorConfig.java @@ -0,0 +1,33 @@ +/* + * Copyright 2017-2022 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micronaut.inject.foreach; + +import io.micronaut.context.annotation.ConfigurationProperties; +import io.micronaut.context.annotation.EachProperty; + +@EachProperty(value = "foo.bar", list = true) +public interface InterfaceAggregatorConfig { + + AddressConfig getAddress(); + + @ConfigurationProperties("address") + interface AddressConfig { + + String getHost(); + + int getPort(); + } +}