From 30e2b2c2fe63593fd2a2827850a051a6b214abab Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 29 Oct 2012 14:51:26 +0100 Subject: [PATCH] Added support for exclusions to AbstractTypeAwareSupport. AbstractTypeAwareSupport now takes an array of type to be excluded from the bean lookup. The BeansOfTypeTaregtSource's actual bean lookup was changed to not trigger bean creation of types to be excluded. --- .../support/AbstractTypeAwareSupport.java | 46 +++++++++++++++---- ...EnablePluginRegistriesIntegrationTest.java | 1 + 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/springframework/plugin/core/support/AbstractTypeAwareSupport.java b/core/src/main/java/org/springframework/plugin/core/support/AbstractTypeAwareSupport.java index f483966..8d3ef7b 100644 --- a/core/src/main/java/org/springframework/plugin/core/support/AbstractTypeAwareSupport.java +++ b/core/src/main/java/org/springframework/plugin/core/support/AbstractTypeAwareSupport.java @@ -16,7 +16,9 @@ package org.springframework.plugin.core.support; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import org.springframework.aop.TargetSource; @@ -42,27 +44,34 @@ public abstract class AbstractTypeAwareSupport implements ApplicationContextA private ApplicationContext context; private Class type; private BeansOfTypeTargetSource targetSource; + private Collection> exclusions; /* * (non-Javadoc) - * - * @see - * org.springframework.context.ApplicationContextAware#setApplicationContext - * (org.springframework.context.ApplicationContext) + * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) */ public void setApplicationContext(ApplicationContext context) { - this.context = context; } /** + * Configures the type of beans to be looked up. + * * @param type the type to set */ public void setType(Class type) { - this.type = type; } + /** + * Configures the types to be excluded from the lookup. + * + * @param exclusions + */ + public void setExclusions(Class[] exclusions) { + this.exclusions = Arrays.asList(exclusions); + } + /** * Returns all beans from the {@link ApplicationContext} that match the given type. * @@ -80,7 +89,7 @@ public abstract class AbstractTypeAwareSupport implements ApplicationContextA * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() { - this.targetSource = new BeansOfTypeTargetSource(context, type, false); + this.targetSource = new BeansOfTypeTargetSource(context, type, false, exclusions); } /* @@ -107,9 +116,10 @@ public abstract class AbstractTypeAwareSupport implements ApplicationContextA private final ListableBeanFactory context; private final Class type; private final boolean eagerInit; + private final Collection> exclusions; private boolean frozen = false; - private Collection components; + private Collection components; /** * Creates a new {@link BeansOfTypeTargetSource} using the given {@link ListableBeanFactory} to lookup beans of the @@ -119,7 +129,8 @@ public abstract class AbstractTypeAwareSupport implements ApplicationContextA * @param type must not be {@literal null}. * @param eagerInit whether to eagerly init {@link FactoryBean}s, defaults to {@literal false}. */ - public BeansOfTypeTargetSource(ListableBeanFactory context, Class type, boolean eagerInit) { + public BeansOfTypeTargetSource(ListableBeanFactory context, Class type, boolean eagerInit, + Collection> exclusions) { Assert.notNull(context); Assert.notNull(type); @@ -127,6 +138,7 @@ public abstract class AbstractTypeAwareSupport implements ApplicationContextA this.context = context; this.type = type; this.eagerInit = eagerInit; + this.exclusions = exclusions == null ? Collections.> emptySet() : exclusions; } /** @@ -160,7 +172,7 @@ public abstract class AbstractTypeAwareSupport implements ApplicationContextA @SuppressWarnings({ "rawtypes", "unchecked" }) public synchronized Object getTarget() throws Exception { - Collection components = this.components == null ? context.getBeansOfType(type, false, eagerInit).values() + Collection components = this.components == null ? getBeansOfTypeExcept(type, exclusions) : this.components; if (frozen && this.components == null) { @@ -177,5 +189,19 @@ public abstract class AbstractTypeAwareSupport implements ApplicationContextA public void releaseTarget(Object target) throws Exception { } + + private Collection getBeansOfTypeExcept(Class type, Collection> exceptions) { + + List result = new ArrayList(); + + for (String beanName : context.getBeanNamesForType(type, false, eagerInit)) { + if (exceptions.contains(context.getType(beanName))) { + continue; + } + result.add(context.getBean(beanName)); + } + + return result; + } } } diff --git a/core/src/test/java/org/springframework/plugin/core/config/EnablePluginRegistriesIntegrationTest.java b/core/src/test/java/org/springframework/plugin/core/config/EnablePluginRegistriesIntegrationTest.java index 72d0aaf..63f69b3 100644 --- a/core/src/test/java/org/springframework/plugin/core/config/EnablePluginRegistriesIntegrationTest.java +++ b/core/src/test/java/org/springframework/plugin/core/config/EnablePluginRegistriesIntegrationTest.java @@ -59,6 +59,7 @@ public class EnablePluginRegistriesIntegrationTest { assertThat(registry, is(notNullValue())); } + @Qualifier("myQualifier") interface AnotherPlugin extends Plugin { }