Programmatic ObjectProvider retrieval through BeanFactory API

Introduces getBeanProvider(Class) and getBeanProvider(ResolvableType), also narrowing getBean(String, Class) and isTypeMatch(String, Class) to a non-null Class argument and enriching NoUniqueBeanDefinitionException with a full ResolvableType. In addition, ObjectProvider supports iterable/stream access for collection-style resolution of multiple matching beans now, and collection injection falls back to an empty collection in a single-constructor case with non-null arguments.

Issue: SPR-17075
Issue: SPR-11419
Issue: SPR-15338
This commit is contained in:
Juergen Hoeller
2018-07-24 00:42:03 +02:00
parent 82194f4ee0
commit 1603c4ab2f
16 changed files with 918 additions and 100 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -29,6 +29,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.config.NamedBeanHolder;
@@ -161,7 +162,7 @@ class StubWebApplicationContext implements WebApplicationContext {
}
@Override
public <T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException {
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return this.beanFactory.getBean(name, requiredType);
}
@@ -180,6 +181,16 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.getBean(requiredType, args);
}
@Override
public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {
return this.beanFactory.getBeanProvider(requiredType);
}
@Override
public <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType) {
return this.beanFactory.getBeanProvider(requiredType);
}
@Override
public boolean containsBean(String name) {
return this.beanFactory.containsBean(name);
@@ -201,7 +212,7 @@ class StubWebApplicationContext implements WebApplicationContext {
}
@Override
public boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
return this.beanFactory.isTypeMatch(name, typeToMatch);
}