diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java index 27e3df91ad..a801a8e5fb 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java @@ -264,7 +264,7 @@ public interface BeanFactory { *
Translates aliases back to the corresponding canonical bean name. * Will ask the parent factory if the bean cannot be found in this factory instance. * @param name the name of the bean to query - * @param targetType the type to match against (as a {@code ResolvableType}) + * @param typeToMatch the type to match against (as a {@code ResolvableType}) * @return {@code true} if the bean type matches, * {@code false} if it doesn't match or cannot be determined yet * @throws NoSuchBeanDefinitionException if there is no bean with the given name @@ -272,7 +272,7 @@ public interface BeanFactory { * @see #getBean * @see #getType */ - boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException; + boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException; /** * Check whether the bean with the given name matches the specified type. @@ -281,7 +281,7 @@ public interface BeanFactory { *
Translates aliases back to the corresponding canonical bean name. * Will ask the parent factory if the bean cannot be found in this factory instance. * @param name the name of the bean to query - * @param targetType the type to match against (as a {@code Class}) + * @param typeToMatch the type to match against (as a {@code Class}) * @return {@code true} if the bean type matches, * {@code false} if it doesn't match or cannot be determined yet * @throws NoSuchBeanDefinitionException if there is no bean with the given name @@ -289,7 +289,7 @@ public interface BeanFactory { * @see #getBean * @see #getType */ - boolean isTypeMatch(String name, Class> targetType) throws NoSuchBeanDefinitionException; + boolean isTypeMatch(String name, Class> typeToMatch) throws NoSuchBeanDefinitionException; /** * Determine the type of the bean with the given name. More specifically, 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 9e08def702..a6af8986c5 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 @@ -560,8 +560,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp } @Override - public boolean isTypeMatch(String name, Class> targetType) throws NoSuchBeanDefinitionException { - return isTypeMatch(name, ResolvableType.forClass(targetType)); + public boolean isTypeMatch(String name, Class> typeToMatch) throws NoSuchBeanDefinitionException { + return isTypeMatch(name, ResolvableType.forClass(typeToMatch != null ? typeToMatch : Object.class)); } @Override diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java index 4d308d7692..2d701ce7fa 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java @@ -169,15 +169,15 @@ public class StaticListableBeanFactory implements ListableBeanFactory { } @Override - public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException { + public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException { Class> type = getType(name); - return (targetType == null || (type != null && targetType.isAssignableFrom(type))); + return (type != null && typeToMatch.isAssignableFrom(type)); } @Override - public boolean isTypeMatch(String name, Class> targetType) throws NoSuchBeanDefinitionException { + public boolean isTypeMatch(String name, Class> typeToMatch) throws NoSuchBeanDefinitionException { Class> type = getType(name); - return (targetType == null || (type != null && targetType.isAssignableFrom(type))); + return (typeToMatch == null || (type != null && typeToMatch.isAssignableFrom(type))); } @Override diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index dc2a234d1f..ed0ef011b7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -1666,6 +1666,36 @@ public class DefaultListableBeanFactoryTests { assertNull(lbf.getType("factoryBean")); } + @Test + public void testGetBeanNamesForTypeBeforeFactoryBeanCreation() { + DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName())); + assertFalse(lbf.containsSingleton("factoryBean")); + + String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false); + assertEquals(1, beanNames.length); + assertEquals("&factoryBean", beanNames[0]); + + beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false); + assertEquals(1, beanNames.length); + assertEquals("&factoryBean", beanNames[0]); + } + + @Test + public void testGetBeanNamesForTypeAfterFactoryBeanCreation() { + DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName())); + lbf.getBean("&factoryBean"); + + String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false); + assertEquals(1, beanNames.length); + assertEquals("&factoryBean", beanNames[0]); + + beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false); + assertEquals(1, beanNames.length); + assertEquals("&factoryBean", beanNames[0]); + } + /** * Verifies that a dependency on a {@link FactoryBean} can not * be autowired by name, as & is an illegal character in @@ -2862,7 +2892,7 @@ public class DefaultListableBeanFactoryTests { } - public static class FactoryBeanThatShouldntBeCalled implements FactoryBean