Skip to content

Commit

Permalink
SmartObjectFactory provides getObject(args) variant as well
Browse files Browse the repository at this point in the history
Issue: SPR-13956
  • Loading branch information
jhoeller committed Feb 19, 2016
1 parent 5ed9046 commit 890819f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,29 @@
public interface SmartObjectFactory<T> extends ObjectFactory<T> {

/**
* Return an instance (possibly shared or independent)
* of the object managed by this factory.
* Return an instance (possibly shared or independent) of the object
* managed by this factory.
* <p>Allows for specifying explicit construction arguments, along the
* lines of {@link BeanFactory#getBean(String, Object...)}.
* @param args arguments to use when creating a corresponding instance
* @return an instance of the bean
* @throws BeansException in case of creation errors
* @see #getObject()
*/
T getObject(Object... args) throws BeansException;

/**
* Return an instance (possibly shared or independent) of the object
* managed by this factory.
* @return an instance of the bean, or {@code null} if not available
* @throws BeansException in case of creation errors
* @see #getObject()
*/
T getIfAvailable() throws BeansException;

/**
* Return an instance (possibly shared or independent)
* of the object managed by this factory.
* Return an instance (possibly shared or independent) of the object
* managed by this factory.
* @return an instance of the bean, or {@code null} if not available or
* not unique (i.e. multiple candidates found with none marked as primary)
* @throws BeansException in case of creation errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.core.GenericCollectionTypeResolver;
import org.springframework.core.GenericTypeResolver;
Expand Down Expand Up @@ -200,6 +201,20 @@ public Object resolveNotUnique(Class<?> type, Map<String, Object> matchingBeans)
throw new NoUniqueBeanDefinitionException(type, matchingBeans.keySet());
}

/**
* Resolve the specified bean name, as a candidate result of the matching
* algorithm for this dependency, to a bean instance from the given factory.
* <p>The default implementation calls {@link BeanFactory#getBean(String)}.
* Subclasses may provide additional arguments or other customizations.
* @param beanName the bean name, as a candidate result for this dependency
* @param beanFactory the associated factory
* @return the bean instance (never {@code null})
* @see BeanFactory#getBean(String)
*/
public Object resolveCandidate(String beanName, BeanFactory beanFactory) {
return beanFactory.getBean(beanName);
}


/**
* Increase this descriptor's nesting level.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ public Object doResolveDependency(DependencyDescriptor descriptor, String beanNa
if (matchingBeans.size() > 1) {
String primaryBeanName = determineAutowireCandidate(matchingBeans, descriptor);
if (primaryBeanName == null) {
if (!indicatesMultipleBeans(type) || descriptor.isRequired()) {
if (descriptor.isRequired() || !indicatesMultipleBeans(type)) {
return descriptor.resolveNotUnique(type, matchingBeans);
}
else {
Expand Down Expand Up @@ -1198,22 +1198,22 @@ protected Map<String, Object> findAutowireCandidates(
}
for (String candidateName : candidateNames) {
if (!isSelfReference(beanName, candidateName) && isAutowireCandidate(candidateName, descriptor)) {
result.put(candidateName, getBean(candidateName));
result.put(candidateName, descriptor.resolveCandidate(candidateName, this));
}
}
if (result.isEmpty() && !indicatesMultipleBeans(requiredType)) {
// Consider fallback matches if the first pass failed to find anything...
DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch();
for (String candidateName : candidateNames) {
if (!isSelfReference(beanName, candidateName) && isAutowireCandidate(candidateName, fallbackDescriptor)) {
result.put(candidateName, getBean(candidateName));
result.put(candidateName, descriptor.resolveCandidate(candidateName, this));
}
}
if (result.isEmpty()) {
// Consider self references before as a final pass
for (String candidateName : candidateNames) {
if (isSelfReference(beanName, candidateName) && isAutowireCandidate(candidateName, fallbackDescriptor)) {
result.put(candidateName, getBean(candidateName));
result.put(candidateName, descriptor.resolveCandidate(candidateName, this));
}
}
}
Expand Down Expand Up @@ -1462,12 +1462,17 @@ private Object readResolve() {
@UsesJava8
private class OptionalDependencyFactory {

public Object createOptionalDependency(DependencyDescriptor descriptor, String beanName) {
public Object createOptionalDependency(DependencyDescriptor descriptor, String beanName, final Object... args) {
DependencyDescriptor descriptorToUse = new DependencyDescriptor(descriptor) {
@Override
public boolean isRequired() {
return false;
}
@Override
public Object resolveCandidate(String beanName, BeanFactory beanFactory) {
return (!ObjectUtils.isEmpty(args) ? beanFactory.getBean(beanName, args) :
super.resolveCandidate(beanName, beanFactory));
}
};
descriptorToUse.increaseNestingLevel();
return Optional.ofNullable(doResolveDependency(descriptorToUse, beanName, null, null));
Expand Down Expand Up @@ -1503,6 +1508,22 @@ public Object getObject() throws BeansException {
}
}

@Override
public Object getObject(final Object... args) throws BeansException {
if (this.optional) {
return new OptionalDependencyFactory().createOptionalDependency(this.descriptor, this.beanName, args);
}
else {
DependencyDescriptor descriptorToUse = new DependencyDescriptor(descriptor) {
@Override
public Object resolveCandidate(String beanName, BeanFactory beanFactory) {
return beanFactory.getBean(beanName, args);
}
};
return doResolveDependency(descriptorToUse, this.beanName, null, null);
}
}

@Override
public Object getIfAvailable() throws BeansException {
if (this.optional) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,26 @@ public void testObjectFactorySerialization() throws Exception {
}

@Test
public void testSmartObjectFactoryInjection() {
public void testSmartObjectFactoryInjectionWithPrototype() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SmartObjectFactoryInjectionBean.class));
RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("testBean", tbd);

SmartObjectFactoryInjectionBean bean = (SmartObjectFactoryInjectionBean) bf.getBean("annotatedBean");
assertEquals(bf.getBean("testBean"), bean.getTestBean());
assertEquals(bf.getBean("testBean", "myName"), bean.getTestBean("myName"));
assertEquals(bf.getBean("testBean"), bean.getOptionalTestBean());
assertEquals(bf.getBean("testBean"), bean.getUniqueTestBean());
bf.destroySingletons();
}

@Test
public void testSmartObjectFactoryInjectionWithSingletonTarget() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
Expand Down Expand Up @@ -2557,6 +2576,10 @@ public TestBean getTestBean() {
return this.testBeanFactory.getObject();
}

public TestBean getTestBean(String name) {
return this.testBeanFactory.getObject(name);
}

public TestBean getOptionalTestBean() {
return this.testBeanFactory.getIfAvailable();
}
Expand Down

0 comments on commit 890819f

Please sign in to comment.