Skip to content

Commit

Permalink
Skip abstract beans in getBeansWithAnnotation()
Browse files Browse the repository at this point in the history
Update BeanFactory.getBeansWithAnnotation() to skip any abstract
bean definitions.

Issue: SPR-10663
  • Loading branch information
philwebb committed Jun 21, 2013
1 parent a403e8f commit 8ed8ee2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -469,11 +468,14 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle

@Override
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
Set<String> beanNames = new LinkedHashSet<String>(getBeanDefinitionCount());
beanNames.addAll(Arrays.asList(getBeanDefinitionNames()));
beanNames.addAll(Arrays.asList(getSingletonNames()));
Map<String, Object> results = new LinkedHashMap<String, Object>();
for (String beanName : beanNames) {
for (String beanName : getBeanDefinitionNames()) {
BeanDefinition beanDefinition = getBeanDefinition(beanName);
if (!beanDefinition.isAbstract() && (findAnnotationOnBean(beanName, annotationType) != null)) {
results.put(beanName, getBean(beanName));
}
}
for (String beanName : getSingletonNames()) {
if (findAnnotationOnBean(beanName, annotationType) != null) {
results.put(beanName, getBean(beanName));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="abstractFactoryBean" class="org.springframework.beans.factory.FactoryBeanTests$AbstractFactoryBean" abstract="true"/>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
Expand All @@ -38,6 +39,7 @@ public final class FactoryBeanTests {
private static final Class<?> CLASS = FactoryBeanTests.class;
private static final Resource RETURNS_NULL_CONTEXT = qualifiedResource(CLASS, "returnsNull.xml");
private static final Resource WITH_AUTOWIRING_CONTEXT = qualifiedResource(CLASS, "withAutowiring.xml");
private static final Resource ABSTRACT_CONTEXT = qualifiedResource(CLASS, "abstract.xml");

@Test
public void testFactoryBeanReturnsNull() throws Exception {
Expand Down Expand Up @@ -80,6 +82,20 @@ public void testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure() throw
assertSame(gamma, beta.getGamma());
}

@Test
public void testAbstractFactoryBeanViaAnnotation() throws Exception {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(ABSTRACT_CONTEXT);
factory.getBeansWithAnnotation(Component.class);
}

@Test
public void testAbstractFactoryBeanViaType() throws Exception {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(ABSTRACT_CONTEXT);
factory.getBeansOfType(AbstractFactoryBean.class);
}


public static class NullReturningFactoryBean implements FactoryBean<Object> {

Expand Down Expand Up @@ -152,6 +168,7 @@ public static class Gamma {
}


@Component
public static class BetaFactoryBean implements FactoryBean<Object> {

private Beta beta;
Expand All @@ -176,4 +193,7 @@ public boolean isSingleton() {
}
}

public abstract static class AbstractFactoryBean implements FactoryBean<Object> {
}

}

0 comments on commit 8ed8ee2

Please sign in to comment.