BeanFactory supports bean creation arguments for by-type lookup as well
Issue: SPR-11235
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -181,6 +181,27 @@ public interface BeanFactory {
|
||||
*/
|
||||
Object getBean(String name, Object... args) throws BeansException;
|
||||
|
||||
/**
|
||||
* Return an instance, which may be shared or independent, of the specified bean.
|
||||
* <p>Allows for specifying explicit constructor arguments / factory method arguments,
|
||||
* overriding the specified default arguments (if any) in the bean definition.
|
||||
* @param requiredType type the bean must match; can be an interface or superclass.
|
||||
* {@code null} is disallowed.
|
||||
* <p>This method goes into {@link ListableBeanFactory} by-type lookup territory
|
||||
* but may also be translated into a conventional by-name lookup based on the name
|
||||
* of the given type. For more extensive retrieval operations across sets of beans,
|
||||
* use {@link ListableBeanFactory} and/or {@link BeanFactoryUtils}.
|
||||
* @param args arguments to use if creating a prototype using explicit arguments to a
|
||||
* static factory method. It is invalid to use a non-null args value in any other case.
|
||||
* @return an instance of the bean
|
||||
* @throws NoSuchBeanDefinitionException if there is no such bean definition
|
||||
* @throws BeanDefinitionStoreException if arguments have been given but
|
||||
* the affected bean isn't a prototype
|
||||
* @throws BeansException if the bean could not be created
|
||||
* @since 4.1
|
||||
*/
|
||||
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
|
||||
|
||||
/**
|
||||
* Does this bean factory contain a bean definition or externally registered singleton
|
||||
* instance with the given name?
|
||||
|
||||
@@ -295,6 +295,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
|
||||
@Override
|
||||
public <T> T getBean(Class<T> requiredType) throws BeansException {
|
||||
return getBean(requiredType, (Object[]) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
|
||||
Assert.notNull(requiredType, "Required type must not be null");
|
||||
String[] beanNames = getBeanNamesForType(requiredType);
|
||||
if (beanNames.length > 1) {
|
||||
@@ -309,25 +314,25 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
}
|
||||
}
|
||||
if (beanNames.length == 1) {
|
||||
return getBean(beanNames[0], requiredType);
|
||||
return getBean(beanNames[0], requiredType, args);
|
||||
}
|
||||
else if (beanNames.length > 1) {
|
||||
Map<String, Object> candidates = new HashMap<String, Object>();
|
||||
for (String beanName : beanNames) {
|
||||
candidates.put(beanName, getBean(beanName, requiredType));
|
||||
candidates.put(beanName, getBean(beanName, requiredType, args));
|
||||
}
|
||||
String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
|
||||
if (primaryCandidate != null) {
|
||||
return getBean(primaryCandidate, requiredType);
|
||||
return getBean(primaryCandidate, requiredType, args);
|
||||
}
|
||||
String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
|
||||
if (priorityCandidate != null) {
|
||||
return getBean(priorityCandidate, requiredType);
|
||||
return getBean(priorityCandidate, requiredType, args);
|
||||
}
|
||||
throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());
|
||||
}
|
||||
else if (getParentBeanFactory() != null) {
|
||||
return getParentBeanFactory().getBean(requiredType);
|
||||
return getParentBeanFactory().getBean(requiredType, args);
|
||||
}
|
||||
else {
|
||||
throw new NoSuchBeanDefinitionException(requiredType);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -133,11 +133,20 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
public Object getBean(String name, Object... args) throws BeansException {
|
||||
if (args != null) {
|
||||
throw new UnsupportedOperationException(
|
||||
"StaticListableBeanFactory does not support explicit bean creation arguments)");
|
||||
"StaticListableBeanFactory does not support explicit bean creation arguments");
|
||||
}
|
||||
return getBean(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
|
||||
if (args != null) {
|
||||
throw new UnsupportedOperationException(
|
||||
"StaticListableBeanFactory does not support explicit bean creation arguments");
|
||||
}
|
||||
return getBean(requiredType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsBean(String name) {
|
||||
return this.beans.containsKey(name);
|
||||
|
||||
Reference in New Issue
Block a user