ResolvableType-based type matching at the BeanFactory API level

Issue: SPR-12147
This commit is contained in:
Juergen Hoeller 2015-03-18 23:05:13 +01:00
parent a3e5fbf5ed
commit 778a01943b
14 changed files with 323 additions and 102 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.beans.factory; package org.springframework.beans.factory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
/** /**
* The root interface for accessing a Spring bean container. * The root interface for accessing a Spring bean container.
@ -114,6 +115,7 @@ public interface BeanFactory {
*/ */
String FACTORY_BEAN_PREFIX = "&"; String FACTORY_BEAN_PREFIX = "&";
/** /**
* Return an instance, which may be shared or independent, of the specified bean. * Return an instance, which may be shared or independent, of the specified bean.
* <p>This method allows a Spring BeanFactory to be used as a replacement for the * <p>This method allows a Spring BeanFactory to be used as a replacement for the
@ -202,6 +204,7 @@ public interface BeanFactory {
*/ */
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException; <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
/** /**
* Does this bean factory contain a bean definition or externally registered singleton * Does this bean factory contain a bean definition or externally registered singleton
* instance with the given name? * instance with the given name?
@ -261,7 +264,24 @@ public interface BeanFactory {
* <p>Translates aliases back to the corresponding canonical bean name. * <p>Translates aliases back to the corresponding canonical bean name.
* Will ask the parent factory if the bean cannot be found in this factory instance. * 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 name the name of the bean to query
* @param targetType the type to match against * @param targetType 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
* @since 4.2
* @see #getBean
* @see #getType
*/
boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException;
/**
* Check whether the bean with the given name matches the specified type.
* More specifically, check whether a {@link #getBean} call for the given name
* would return an object that is assignable to the specified target type.
* <p>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})
* @return {@code true} if the bean type matches, * @return {@code true} if the bean type matches,
* {@code false} if it doesn't match or cannot be determined yet * {@code false} if it doesn't match or cannot be determined yet
* @throws NoSuchBeanDefinitionException if there is no bean with the given name * @throws NoSuchBeanDefinitionException if there is no bean with the given name

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
package org.springframework.beans.factory; package org.springframework.beans.factory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -24,6 +23,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -126,6 +126,39 @@ public abstract class BeanFactoryUtils {
return beanNamesForTypeIncludingAncestors(lbf, Object.class); return beanNamesForTypeIncludingAncestors(lbf, Object.class);
} }
/**
* Get all bean names for the given type, including those defined in ancestor
* factories. Will return unique names in case of overridden bean definitions.
* <p>Does consider objects created by FactoryBeans, which means that FactoryBeans
* will get initialized. If the object created by the FactoryBean doesn't match,
* the raw FactoryBean itself will be matched against the type.
* <p>This version of {@code beanNamesForTypeIncludingAncestors} automatically
* includes prototypes and FactoryBeans.
* @param lbf the bean factory
* @param type the type that beans must match (as a {@code ResolvableType})
* @return the array of matching bean names, or an empty array if none
* @since 4.2
*/
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, ResolvableType type) {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
String[] result = lbf.getBeanNamesForType(type);
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
String[] parentResult = beanNamesForTypeIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), type);
List<String> resultList = new ArrayList<String>();
resultList.addAll(Arrays.asList(result));
for (String beanName : parentResult) {
if (!resultList.contains(beanName) && !hbf.containsLocalBean(beanName)) {
resultList.add(beanName);
}
}
result = StringUtils.toStringArray(resultList);
}
}
return result;
}
/** /**
* Get all bean names for the given type, including those defined in ancestor * Get all bean names for the given type, including those defined in ancestor
@ -136,7 +169,7 @@ public abstract class BeanFactoryUtils {
* <p>This version of {@code beanNamesForTypeIncludingAncestors} automatically * <p>This version of {@code beanNamesForTypeIncludingAncestors} automatically
* includes prototypes and FactoryBeans. * includes prototypes and FactoryBeans.
* @param lbf the bean factory * @param lbf the bean factory
* @param type the type that beans must match * @param type the type that beans must match (as a {@code Class})
* @return the array of matching bean names, or an empty array if none * @return the array of matching bean names, or an empty array if none
*/ */
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type) { public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.lang.annotation.Annotation;
import java.util.Map; import java.util.Map;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
/** /**
* Extension of the {@link BeanFactory} interface to be implemented by bean factories * Extension of the {@link BeanFactory} interface to be implemented by bean factories
@ -85,6 +86,34 @@ public interface ListableBeanFactory extends BeanFactory {
*/ */
String[] getBeanDefinitionNames(); String[] getBeanDefinitionNames();
/**
* Return the names of beans matching the given type (including subclasses),
* judging from either bean definitions or the value of {@code getObjectType}
* in the case of FactoryBeans.
* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>
* check nested beans which might match the specified type as well.
* <p>Does consider objects created by FactoryBeans, which means that FactoryBeans
* will get initialized. If the object created by the FactoryBean doesn't match,
* the raw FactoryBean itself will be matched against the type.
* <p>Does not consider any hierarchy this factory may participate in.
* Use BeanFactoryUtils' {@code beanNamesForTypeIncludingAncestors}
* to include beans in ancestor factories too.
* <p>Note: Does <i>not</i> ignore singleton beans that have been registered
* by other means than bean definitions.
* <p>This version of {@code getBeanNamesForType} matches all kinds of beans,
* be it singletons, prototypes, or FactoryBeans. In most implementations, the
* result will be the same as for {@code getBeanNamesForType(type, true, true)}.
* <p>Bean names returned by this method should always return bean names <i>in the
* order of definition</i> in the backend configuration, as far as possible.
* @param type the class or interface to match, or {@code null} for all bean names
* @return the names of beans (or objects created by FactoryBeans) matching
* the given object type (including subclasses), or an empty array if none
* @since 4.2
* @see FactoryBean#getObjectType
* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)
*/
String[] getBeanNamesForType(ResolvableType type);
/** /**
* Return the names of beans matching the given type (including subclasses), * Return the names of beans matching the given type (including subclasses),
* judging from either bean definitions or the value of {@code getObjectType} * judging from either bean definitions or the value of {@code getObjectType}

View File

@ -68,6 +68,7 @@ import org.springframework.beans.factory.config.InstantiationAwareBeanPostProces
import org.springframework.beans.factory.config.Scope; import org.springframework.beans.factory.config.Scope;
import org.springframework.core.DecoratingClassLoader; import org.springframework.core.DecoratingClassLoader;
import org.springframework.core.NamedThreadLocal; import org.springframework.core.NamedThreadLocal;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -479,9 +480,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
String beanName = transformedBeanName(name); String beanName = transformedBeanName(name);
Class<?> typeToMatch = (targetType != null ? targetType : Object.class);
// Check manually registered singletons. // Check manually registered singletons.
Object beanInstance = getSingleton(beanName, false); Object beanInstance = getSingleton(beanName, false);
@ -489,15 +489,14 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
if (beanInstance instanceof FactoryBean) { if (beanInstance instanceof FactoryBean) {
if (!BeanFactoryUtils.isFactoryDereference(name)) { if (!BeanFactoryUtils.isFactoryDereference(name)) {
Class<?> type = getTypeForFactoryBean((FactoryBean<?>) beanInstance); Class<?> type = getTypeForFactoryBean((FactoryBean<?>) beanInstance);
return (type != null && ClassUtils.isAssignable(typeToMatch, type)); return (type != null && typeToMatch.isAssignableFrom(type));
} }
else { else {
return ClassUtils.isAssignableValue(typeToMatch, beanInstance); return typeToMatch.isInstance(beanInstance);
} }
} }
else { else {
return !BeanFactoryUtils.isFactoryDereference(name) && return (!BeanFactoryUtils.isFactoryDereference(name) && typeToMatch.isInstance(beanInstance));
ClassUtils.isAssignableValue(typeToMatch, beanInstance);
} }
} }
else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) { else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
@ -510,14 +509,15 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
BeanFactory parentBeanFactory = getParentBeanFactory(); BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// No bean definition found in this factory -> delegate to parent. // No bean definition found in this factory -> delegate to parent.
return parentBeanFactory.isTypeMatch(originalBeanName(name), targetType); return parentBeanFactory.isTypeMatch(originalBeanName(name), typeToMatch);
} }
// Retrieve corresponding bean definition. // Retrieve corresponding bean definition.
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
Class<?>[] typesToMatch = (FactoryBean.class.equals(typeToMatch) ? Class<?> classToMatch = typeToMatch.getRawClass();
new Class<?>[] {typeToMatch} : new Class<?>[] {FactoryBean.class, typeToMatch}); Class<?>[] typesToMatch = (FactoryBean.class.equals(classToMatch) ?
new Class<?>[] {classToMatch} : new Class<?>[] {FactoryBean.class, classToMatch});
// Check decorated bean definition, if any: We assume it'll be easier // Check decorated bean definition, if any: We assume it'll be easier
// to determine the decorated bean's type than the proxy's type. // to determine the decorated bean's type than the proxy's type.
@ -559,6 +559,11 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
} }
@Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
return isTypeMatch(name, ResolvableType.forClass(targetType));
}
@Override @Override
public Class<?> getType(String name) throws NoSuchBeanDefinitionException { public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
String beanName = transformedBeanName(name); String beanName = transformedBeanName(name);

View File

@ -66,6 +66,7 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.core.OrderComparator; import org.springframework.core.OrderComparator;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.UsesJava8; import org.springframework.lang.UsesJava8;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -323,7 +324,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Implementation of ListableBeanFactory interface // Implementation of remaining BeanFactory methods
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@Override @Override
@ -372,6 +373,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
} }
} }
//---------------------------------------------------------------------
// Implementation of ListableBeanFactory interface
//---------------------------------------------------------------------
@Override @Override
public boolean containsBeanDefinition(String beanName) { public boolean containsBeanDefinition(String beanName) {
Assert.notNull(beanName, "Bean name must not be null"); Assert.notNull(beanName, "Bean name must not be null");
@ -393,6 +399,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
} }
} }
@Override
public String[] getBeanNamesForType(ResolvableType type) {
return doGetBeanNamesForType(type, true, true);
}
@Override @Override
public String[] getBeanNamesForType(Class<?> type) { public String[] getBeanNamesForType(Class<?> type) {
return getBeanNamesForType(type, true, true); return getBeanNamesForType(type, true, true);
@ -401,7 +412,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
@Override @Override
public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) { public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
if (!isConfigurationFrozen() || type == null || !allowEagerInit) { if (!isConfigurationFrozen() || type == null || !allowEagerInit) {
return doGetBeanNamesForType(type, includeNonSingletons, allowEagerInit); return doGetBeanNamesForType(ResolvableType.forClass(type != null ? type : Object.class),
includeNonSingletons, allowEagerInit);
} }
Map<Class<?>, String[]> cache = Map<Class<?>, String[]> cache =
(includeNonSingletons ? this.allBeanNamesByType : this.singletonBeanNamesByType); (includeNonSingletons ? this.allBeanNamesByType : this.singletonBeanNamesByType);
@ -409,14 +421,14 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (resolvedBeanNames != null) { if (resolvedBeanNames != null) {
return resolvedBeanNames; return resolvedBeanNames;
} }
resolvedBeanNames = doGetBeanNamesForType(type, includeNonSingletons, allowEagerInit); resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forClass(type), includeNonSingletons, allowEagerInit);
if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) { if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) {
cache.put(type, resolvedBeanNames); cache.put(type, resolvedBeanNames);
} }
return resolvedBeanNames; return resolvedBeanNames;
} }
private String[] doGetBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) { private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
// Check all bean definitions. // Check all bean definitions.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,6 +33,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.SmartFactoryBean; import org.springframework.beans.factory.SmartFactoryBean;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -167,6 +168,12 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
(bean instanceof FactoryBean && !((FactoryBean<?>) bean).isSingleton())); (bean instanceof FactoryBean && !((FactoryBean<?>) bean).isSingleton()));
} }
@Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name);
return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
}
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name); Class<?> type = getType(name);
@ -216,24 +223,18 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
} }
@Override @Override
public String[] getBeanNamesForType(Class<?> type) { public String[] getBeanNamesForType(ResolvableType type) {
return getBeanNamesForType(type, true, true); boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type.getRawClass()));
}
@Override
public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean includeFactoryBeans) {
boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type));
List<String> matches = new ArrayList<String>(); List<String> matches = new ArrayList<String>();
for (String name : this.beans.keySet()) { for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
Object beanInstance = this.beans.get(name); String name = entry.getKey();
Object beanInstance = entry.getValue();
if (beanInstance instanceof FactoryBean && !isFactoryType) { if (beanInstance instanceof FactoryBean && !isFactoryType) {
if (includeFactoryBeans) {
Class<?> objectType = ((FactoryBean<?>) beanInstance).getObjectType(); Class<?> objectType = ((FactoryBean<?>) beanInstance).getObjectType();
if (objectType != null && (type == null || type.isAssignableFrom(objectType))) { if (objectType != null && (type == null || type.isAssignableFrom(objectType))) {
matches.add(name); matches.add(name);
} }
} }
}
else { else {
if (type == null || type.isInstance(beanInstance)) { if (type == null || type.isInstance(beanInstance)) {
matches.add(name); matches.add(name);
@ -243,6 +244,16 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
return StringUtils.toStringArray(matches); return StringUtils.toStringArray(matches);
} }
@Override
public String[] getBeanNamesForType(Class<?> type) {
return getBeanNamesForType(ResolvableType.forClass(type));
}
@Override
public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
return getBeanNamesForType(ResolvableType.forClass(type));
}
@Override @Override
public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException { public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
return getBeansOfType(type, true, true); return getBeansOfType(type, true, true);
@ -250,18 +261,17 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean includeFactoryBeans) public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
throws BeansException { throws BeansException {
boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type)); boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type));
Map<String, T> matches = new HashMap<String, T>(); Map<String, T> matches = new HashMap<String, T>();
for (Map.Entry<String, Object> entry : beans.entrySet()) { for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
String beanName = entry.getKey(); String beanName = entry.getKey();
Object beanInstance = entry.getValue(); Object beanInstance = entry.getValue();
// Is bean a FactoryBean? // Is bean a FactoryBean?
if (beanInstance instanceof FactoryBean && !isFactoryType) { if (beanInstance instanceof FactoryBean && !isFactoryType) {
if (includeFactoryBeans) {
// Match object created by FactoryBean. // Match object created by FactoryBean.
FactoryBean<?> factory = (FactoryBean<?>) beanInstance; FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
Class<?> objectType = factory.getObjectType(); Class<?> objectType = factory.getObjectType();
@ -270,7 +280,6 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
matches.put(beanName, getBean(beanName, type)); matches.put(beanName, getBean(beanName, type));
} }
} }
}
else { else {
if (type == null || type.isInstance(beanInstance)) { if (type == null || type.isInstance(beanInstance)) {
// If type to match is FactoryBean, return FactoryBean itself. // If type to match is FactoryBean, return FactoryBean itself.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,8 +23,6 @@ import java.util.Map;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.StaticListableBeanFactory; import org.springframework.beans.factory.support.StaticListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@ -53,16 +51,16 @@ public final class BeanFactoryUtilsTests {
private static final Resource LEAF_CONTEXT = qualifiedResource(CLASS, "leaf.xml"); private static final Resource LEAF_CONTEXT = qualifiedResource(CLASS, "leaf.xml");
private static final Resource DEPENDENT_BEANS_CONTEXT = qualifiedResource(CLASS, "dependentBeans.xml"); private static final Resource DEPENDENT_BEANS_CONTEXT = qualifiedResource(CLASS, "dependentBeans.xml");
private ConfigurableListableBeanFactory listableBeanFactory; private DefaultListableBeanFactory listableBeanFactory;
private DefaultListableBeanFactory dependentBeansFactory;
private ConfigurableListableBeanFactory dependentBeansBF;
@Before @Before
public void setUp() { public void setUp() {
// Interesting hierarchical factory to test counts. // Interesting hierarchical factory to test counts.
// Slow to read so we cache it. // Slow to read so we cache it.
DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory(); DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT); new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory(grandParent); DefaultListableBeanFactory parent = new DefaultListableBeanFactory(grandParent);
@ -70,12 +68,13 @@ public final class BeanFactoryUtilsTests {
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent); DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(LEAF_CONTEXT); new XmlBeanDefinitionReader(child).loadBeanDefinitions(LEAF_CONTEXT);
this.dependentBeansBF = new DefaultListableBeanFactory(); this.dependentBeansFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.dependentBeansBF).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT); new XmlBeanDefinitionReader(this.dependentBeansFactory).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
dependentBeansBF.preInstantiateSingletons(); dependentBeansFactory.preInstantiateSingletons();
this.listableBeanFactory = child; this.listableBeanFactory = child;
} }
@Test @Test
public void testHierarchicalCountBeansWithNonHierarchicalFactory() { public void testHierarchicalCountBeansWithNonHierarchicalFactory() {
StaticListableBeanFactory lbf = new StaticListableBeanFactory(); StaticListableBeanFactory lbf = new StaticListableBeanFactory();
@ -92,22 +91,21 @@ public final class BeanFactoryUtilsTests {
// Leaf count // Leaf count
assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1); assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1);
// Count minus duplicate // Count minus duplicate
assertTrue("Should count 7 beans, not " assertTrue("Should count 7 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
+ BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7); BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
} }
@Test @Test
public void testHierarchicalNamesWithNoMatch() throws Exception { public void testHierarchicalNamesWithNoMatch() throws Exception {
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, List<String> names = Arrays.asList(
NoOp.class)); BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, NoOp.class));
assertEquals(0, names.size()); assertEquals(0, names.size());
} }
@Test @Test
public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception { public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception {
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, List<String> names = Arrays.asList(
IndexedTestBean.class)); BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class));
assertEquals(1, names.size()); assertEquals(1, names.size());
assertTrue(names.contains("indexedBean")); assertTrue(names.contains("indexedBean"));
// Distinguish from default ListableBeanFactory behavior // Distinguish from default ListableBeanFactory behavior
@ -116,8 +114,8 @@ public final class BeanFactoryUtilsTests {
@Test @Test
public void testGetBeanNamesForTypeWithOverride() throws Exception { public void testGetBeanNamesForTypeWithOverride() throws Exception {
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, List<String> names = Arrays.asList(
ITestBean.class)); BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class));
// includes 2 TestBeans from FactoryBeans (DummyFactory definitions) // includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
assertEquals(4, names.size()); assertEquals(4, names.size());
assertTrue(names.contains("test")); assertTrue(names.contains("test"));
@ -147,18 +145,7 @@ public final class BeanFactoryUtilsTests {
lbf.addBean("t3", t3); lbf.addBean("t3", t3);
lbf.addBean("t4", t4); lbf.addBean("t4", t4);
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false); Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
assertEquals(2, beans.size());
assertEquals(t1, beans.get("t1"));
assertEquals(t2, beans.get("t2"));
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, false, true);
assertEquals(3, beans.size());
assertEquals(t1, beans.get("t1"));
assertEquals(t2, beans.get("t2"));
assertEquals(t3.getObject(), beans.get("t3"));
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
assertEquals(4, beans.size()); assertEquals(4, beans.size());
assertEquals(t1, beans.get("t1")); assertEquals(t1, beans.get("t1"));
assertEquals(t2, beans.get("t2")); assertEquals(t2, beans.get("t2"));
@ -191,8 +178,8 @@ public final class BeanFactoryUtilsTests {
this.listableBeanFactory.registerSingleton("t3", t3); this.listableBeanFactory.registerSingleton("t3", t3);
this.listableBeanFactory.registerSingleton("t4", t4); this.listableBeanFactory.registerSingleton("t4", t4);
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, Map<String, ?> beans =
false); BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
assertEquals(6, beans.size()); assertEquals(6, beans.size());
assertEquals(test3, beans.get("test3")); assertEquals(test3, beans.get("test3"));
assertEquals(test, beans.get("test")); assertEquals(test, beans.get("test"));
@ -200,12 +187,9 @@ public final class BeanFactoryUtilsTests {
assertEquals(t2, beans.get("t2")); assertEquals(t2, beans.get("t2"));
assertEquals(t3.getObject(), beans.get("t3")); assertEquals(t3.getObject(), beans.get("t3"));
assertTrue(beans.get("t4") instanceof TestBean); assertTrue(beans.get("t4") instanceof TestBean);
// t3 and t4 are found here as of Spring 2.0, since they are // t3 and t4 are found here as of Spring 2.0, since they are pre-registered
// pre-registered // singleton instances, while testFactory1 and testFactory are *not* found
// singleton instances, while testFactory1 and testFactory are *not* // because they are FactoryBean definitions that haven't been initialized yet.
// found
// because they are FactoryBean definitions that haven't been
// initialized yet.
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
Object testFactory1 = this.listableBeanFactory.getBean("testFactory1"); Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
@ -247,8 +231,8 @@ public final class BeanFactoryUtilsTests {
Object test3 = this.listableBeanFactory.getBean("test3"); Object test3 = this.listableBeanFactory.getBean("test3");
Object test = this.listableBeanFactory.getBean("test"); Object test = this.listableBeanFactory.getBean("test");
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, Map<?, ?> beans =
false); BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
assertEquals(2, beans.size()); assertEquals(2, beans.size());
assertEquals(test3, beans.get("test3")); assertEquals(test3, beans.get("test3"));
assertEquals(test, beans.get("test")); assertEquals(test, beans.get("test"));
@ -283,25 +267,25 @@ public final class BeanFactoryUtilsTests {
@Test @Test
public void testADependencies() { public void testADependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("a"); String[] deps = this.dependentBeansFactory.getDependentBeans("a");
assertTrue(ObjectUtils.isEmpty(deps)); assertTrue(ObjectUtils.isEmpty(deps));
} }
@Test @Test
public void testBDependencies() { public void testBDependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("b"); String[] deps = this.dependentBeansFactory.getDependentBeans("b");
assertTrue(Arrays.equals(new String[] { "c" }, deps)); assertTrue(Arrays.equals(new String[] { "c" }, deps));
} }
@Test @Test
public void testCDependencies() { public void testCDependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("c"); String[] deps = this.dependentBeansFactory.getDependentBeans("c");
assertTrue(Arrays.equals(new String[] { "int", "long" }, deps)); assertTrue(Arrays.equals(new String[] { "int", "long" }, deps));
} }
@Test @Test
public void testIntDependencies() { public void testIntDependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("int"); String[] deps = this.dependentBeansFactory.getDependentBeans("int");
assertTrue(Arrays.equals(new String[] { "buffer" }, deps)); assertTrue(Arrays.equals(new String[] { "buffer" }, deps));
} }

View File

@ -73,6 +73,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ConstructorDependenciesBean; import org.springframework.beans.factory.xml.ConstructorDependenciesBean;
import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
@ -194,13 +195,16 @@ public class DefaultListableBeanFactoryTests {
assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertTrue(lbf.isTypeMatch("x1", TestBean.class));
assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(TestBean.class, lbf.getType("x1"));
assertEquals(DummyFactory.class, lbf.getType("&x1")); assertEquals(DummyFactory.class, lbf.getType("&x1"));
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
} }
@Test @Test
public void testPrototypeSingletonFactoryBeanIgnoredByNonEagerTypeMatching() { public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties(); Properties p = new Properties();
p.setProperty("x1.(class)", DummyFactory.class.getName()); p.setProperty("x1.(class)", DummyFactory.class.getName());
@ -226,6 +230,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertTrue(lbf.isTypeMatch("x1", TestBean.class));
assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(TestBean.class, lbf.getType("x1"));
assertEquals(DummyFactory.class, lbf.getType("&x1")); assertEquals(DummyFactory.class, lbf.getType("&x1"));
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
@ -257,6 +264,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertTrue(lbf.isTypeMatch("x1", TestBean.class));
assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(TestBean.class, lbf.getType("x1"));
assertEquals(DummyFactory.class, lbf.getType("&x1")); assertEquals(DummyFactory.class, lbf.getType("&x1"));
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
@ -1606,6 +1616,8 @@ public class DefaultListableBeanFactoryTests {
assertEquals(1, lbf.getBeanNamesForType(ConstructorDependency.class).length); assertEquals(1, lbf.getBeanNamesForType(ConstructorDependency.class).length);
assertEquals(1, lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length); assertEquals(1, lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length);
assertEquals(1, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)).length);
assertEquals(0, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)).length);
} }
private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) { private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,6 +40,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.TypedStringValue; import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.UrlResource; import org.springframework.core.io.UrlResource;
import org.springframework.tests.Assume; import org.springframework.tests.Assume;
@ -770,7 +771,7 @@ public class BeanFactoryGenericsTests {
} }
@Test @Test
public void testSpr11250() { public void testGenericMatchingWithBeanNameDifferentiation() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setAutowireCandidateResolver(new GenericTypeAwareAutowireCandidateResolver()); bf.setAutowireCandidateResolver(new GenericTypeAwareAutowireCandidateResolver());
@ -780,8 +781,43 @@ public class BeanFactoryGenericsTests {
new RootBeanDefinition(NumberBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false)); new RootBeanDefinition(NumberBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false));
NumberBean nb = bf.getBean(NumberBean.class); NumberBean nb = bf.getBean(NumberBean.class);
assertNotNull(nb.getDoubleStore()); assertSame(bf.getBean("doubleStore"), nb.getDoubleStore());
assertNotNull(nb.getFloatStore()); assertSame(bf.getBean("floatStore"), nb.getFloatStore());
String[] numberStoreNames = bf.getBeanNamesForType(ResolvableType.forClass(NumberStore.class));
String[] doubleStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Double.class));
String[] floatStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Float.class));
assertEquals(2, numberStoreNames.length);
assertEquals("doubleStore", numberStoreNames[0]);
assertEquals("floatStore", numberStoreNames[1]);
assertEquals(0, doubleStoreNames.length);
assertEquals(0, floatStoreNames.length);
}
@Test
public void testGenericMatchingWithFullTypeDifferentiation() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setAutowireCandidateResolver(new GenericTypeAwareAutowireCandidateResolver());
bf.registerBeanDefinition("store1", new RootBeanDefinition(DoubleStore.class));
bf.registerBeanDefinition("store2", new RootBeanDefinition(FloatStore.class));
bf.registerBeanDefinition("numberBean",
new RootBeanDefinition(NumberBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false));
NumberBean nb = bf.getBean(NumberBean.class);
assertSame(bf.getBean("store1"), nb.getDoubleStore());
assertSame(bf.getBean("store2"), nb.getFloatStore());
String[] numberStoreNames = bf.getBeanNamesForType(ResolvableType.forClass(NumberStore.class));
String[] doubleStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Double.class));
String[] floatStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Float.class));
assertEquals(2, numberStoreNames.length);
assertEquals("store1", numberStoreNames[0]);
assertEquals("store2", numberStoreNames[1]);
assertEquals(1, doubleStoreNames.length);
assertEquals("store1", doubleStoreNames[0]);
assertEquals(1, floatStoreNames.length);
assertEquals("store2", floatStoreNames[0]);
} }
@ -851,6 +887,14 @@ public class BeanFactoryGenericsTests {
} }
public static class DoubleStore extends NumberStore<Double> {
}
public static class FloatStore extends NumberStore<Float> {
}
public static class NumberBean { public static class NumberBean {
private final NumberStore<Double> doubleStore; private final NumberStore<Double> doubleStore;

View File

@ -47,13 +47,13 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.HierarchicalMessageSource; import org.springframework.context.HierarchicalMessageSource;
import org.springframework.context.LifecycleProcessor; import org.springframework.context.LifecycleProcessor;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware; import org.springframework.context.MessageSourceAware;
import org.springframework.context.MessageSourceResolvable; import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException; import org.springframework.context.NoSuchMessageException;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.event.ApplicationEventMulticaster; import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
@ -1024,6 +1024,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
return getBeanFactory().isPrototype(name); return getBeanFactory().isPrototype(name);
} }
@Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().isTypeMatch(name, targetType);
}
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive(); assertBeanFactoryActive();
@ -1061,6 +1067,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
return getBeanFactory().getBeanDefinitionNames(); return getBeanFactory().getBeanDefinitionNames();
} }
@Override
public String[] getBeanNamesForType(ResolvableType type) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type);
}
@Override @Override
public String[] getBeanNamesForType(Class<?> type) { public String[] getBeanNamesForType(Class<?> type) {
assertBeanFactoryActive(); assertBeanFactoryActive();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException; import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.core.ResolvableType;
import org.springframework.jndi.JndiLocatorSupport; import org.springframework.jndi.JndiLocatorSupport;
import org.springframework.jndi.TypeMismatchNamingException; import org.springframework.jndi.TypeMismatchNamingException;
@ -172,6 +173,12 @@ public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFac
return !this.shareableResources.contains(name); return !this.shareableResources.contains(name);
} }
@Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name);
return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
}
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name); Class<?> type = getType(name);

View File

@ -179,6 +179,27 @@ public final class ResolvableType implements Serializable {
return (source != null ? source : this.type); return (source != null ? source : this.type);
} }
/**
* Determine whether the given object is an instance of this {@code ResolvableType}.
* @param obj the object to check
* @since 4.2
* @see #isAssignableFrom(Class)
*/
public boolean isInstance(Object obj) {
return (obj != null && isAssignableFrom(obj.getClass()));
}
/**
* Determine whether this {@code ResolvableType} is assignable from the
* specified other type.
* @param other the type to be checked against (as a {@code Class})
* @since 4.2
* @see #isAssignableFrom(ResolvableType)
*/
public boolean isAssignableFrom(Class<?> other) {
return isAssignableFrom(forClass(other), null);
}
/** /**
* Determine whether this {@code ResolvableType} is assignable from the * Determine whether this {@code ResolvableType} is assignable from the
* specified other type. * specified other type.
@ -186,7 +207,7 @@ public final class ResolvableType implements Serializable {
* whether both the {@link #resolve() resolved} {@code Class} is * whether both the {@link #resolve() resolved} {@code Class} is
* {@link Class#isAssignableFrom(Class) assignable from} the given type * {@link Class#isAssignableFrom(Class) assignable from} the given type
* as well as whether all {@link #getGenerics() generics} are assignable. * as well as whether all {@link #getGenerics() generics} are assignable.
* @param other the type to be checked against * @param other the type to be checked against (as a {@code ResolvableType})
* @return {@code true} if the specified other type can be assigned to this * @return {@code true} if the specified other type can be assigned to this
* {@code ResolvableType}; {@code false} otherwise * {@code ResolvableType}; {@code false} otherwise
*/ */

View File

@ -61,6 +61,7 @@ import static org.mockito.BDDMockito.*;
* Tests for {@link ResolvableType}. * Tests for {@link ResolvableType}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Juergen Hoeller
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -882,7 +883,7 @@ public class ResolvableTypeTests {
public void isAssignableFromMustNotBeNull() throws Exception { public void isAssignableFromMustNotBeNull() throws Exception {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Type must not be null"); this.thrown.expectMessage("Type must not be null");
ResolvableType.forClass(Object.class).isAssignableFrom(null); ResolvableType.forClass(Object.class).isAssignableFrom((ResolvableType) null);
} }
@Test @Test
@ -901,6 +902,20 @@ public class ResolvableTypeTests {
assertAssignable(objectType, objectType, charSequenceType, stringType).equalTo(true, true, true); assertAssignable(objectType, objectType, charSequenceType, stringType).equalTo(true, true, true);
assertAssignable(charSequenceType, objectType, charSequenceType, stringType).equalTo(false, true, true); assertAssignable(charSequenceType, objectType, charSequenceType, stringType).equalTo(false, true, true);
assertAssignable(stringType, objectType, charSequenceType, stringType).equalTo(false, false, true); assertAssignable(stringType, objectType, charSequenceType, stringType).equalTo(false, false, true);
assertTrue(objectType.isAssignableFrom(String.class));
assertTrue(objectType.isAssignableFrom(StringBuilder.class));
assertTrue(charSequenceType.isAssignableFrom(String.class));
assertTrue(charSequenceType.isAssignableFrom(StringBuilder.class));
assertTrue(stringType.isAssignableFrom(String.class));
assertFalse(stringType.isAssignableFrom(StringBuilder.class));
assertTrue(objectType.isInstance("a String"));
assertTrue(objectType.isInstance(new StringBuilder("a StringBuilder")));
assertTrue(charSequenceType.isInstance("a String"));
assertTrue(charSequenceType.isInstance(new StringBuilder("a StringBuilder")));
assertTrue(stringType.isInstance("a String"));
assertFalse(stringType.isInstance(new StringBuilder("a StringBuilder")));
} }
@Test @Test

View File

@ -38,6 +38,7 @@ import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable; import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException; import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.DelegatingMessageSource; import org.springframework.context.support.DelegatingMessageSource;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -98,6 +99,7 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.servletContext; return this.servletContext;
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Implementation of ApplicationContext interface // Implementation of ApplicationContext interface
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -146,6 +148,7 @@ class StubWebApplicationContext implements WebApplicationContext {
} }
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Implementation of BeanFactory interface // Implementation of BeanFactory interface
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -190,6 +193,11 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.isPrototype(name); return this.beanFactory.isPrototype(name);
} }
@Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
return this.beanFactory.isTypeMatch(name, targetType);
}
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
return this.beanFactory.isTypeMatch(name, targetType); return this.beanFactory.isTypeMatch(name, targetType);
@ -205,6 +213,7 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.getAliases(name); return this.beanFactory.getAliases(name);
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Implementation of ListableBeanFactory interface // Implementation of ListableBeanFactory interface
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -224,6 +233,11 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.getBeanDefinitionNames(); return this.beanFactory.getBeanDefinitionNames();
} }
@Override
public String[] getBeanNamesForType(ResolvableType type) {
return this.beanFactory.getBeanNamesForType(type);
}
@Override @Override
public String[] getBeanNamesForType(Class<?> type) { public String[] getBeanNamesForType(Class<?> type) {
return this.beanFactory.getBeanNamesForType(type); return this.beanFactory.getBeanNamesForType(type);
@ -265,6 +279,7 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.findAnnotationOnBean(beanName, annotationType); return this.beanFactory.findAnnotationOnBean(beanName, annotationType);
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Implementation of HierarchicalBeanFactory interface // Implementation of HierarchicalBeanFactory interface
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -279,6 +294,7 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.containsBean(name); return this.beanFactory.containsBean(name);
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Implementation of MessageSource interface // Implementation of MessageSource interface
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -298,6 +314,7 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.messageSource.getMessage(resolvable, locale); return this.messageSource.getMessage(resolvable, locale);
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Implementation of ResourceLoader interface // Implementation of ResourceLoader interface
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -312,6 +329,7 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.resourcePatternResolver.getResource(location); return this.resourcePatternResolver.getResource(location);
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Other // Other
//--------------------------------------------------------------------- //---------------------------------------------------------------------