diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index d46b11437b62..2c9b1d03cb7f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -516,6 +516,12 @@ else if (typeToMatch.hasGenerics() && containsBeanDefinition(beanName)) { Class classToMatch = typeToMatch.resolve(); return (classToMatch == null || classToMatch.isInstance(beanInstance)); } + else if (mbd.targetType == null) { + ResolvableType resolvableType = mbd.factoryMethodReturnType; + if (resolvableType != null) { + return typeToMatch.isAssignableFrom(resolvableType); + } + } } } return false; diff --git a/spring-context/src/test/java/org/springframework/beans/factory/support/SPR17524Tests.java b/spring-context/src/test/java/org/springframework/beans/factory/support/SPR17524Tests.java new file mode 100644 index 000000000000..a9dd9d29cabe --- /dev/null +++ b/spring-context/src/test/java/org/springframework/beans/factory/support/SPR17524Tests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2018 the original author or 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 + * + * http://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 org.springframework.beans.factory.support; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.ResolvableType; + +import static org.junit.Assert.assertEquals; + +/** + * @author Melnikov Nikita + */ +public class SPR17524Tests { + + @Test + public void testNonLazyWithGeneric() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Configuration.class); + ResolvableType type = ResolvableType.forClassWithGenerics(List.class, String.class); + assertEquals(context.getBeanNamesForType(type).length, 1); + } + + @Test + public void testNonLazyWithGenericMultipleCalls() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Configuration.class); + ResolvableType type = ResolvableType.forClassWithGenerics(List.class, String.class); + assertEquals(context.getBeanNamesForType(type).length, 1); + assertEquals(context.getBeanNamesForType(type).length, 1); + assertEquals(context.getBeanNamesForType(type).length, 1); + } + + @Test + public void testLazyWithGenericMultipleCalls() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Configuration.class); + ResolvableType type = ResolvableType.forClassWithGenerics(List.class, Integer.class); + assertEquals(context.getBeanNamesForType(type).length, 1); + assertEquals(context.getBeanNamesForType(type).length, 1); + assertEquals(context.getBeanNamesForType(type).length, 1); + } + + static class Configuration { + + @Bean + public List stringListBean() { + return new ArrayList<>(); + } + + @Bean + @Lazy + public List lazyIntegerListBean() { + return new ArrayList<>(); + } + } +}