Introduce getType variant with allowFactoryBeanInit flag
Closes gh-23374
This commit is contained in:
@@ -323,7 +323,8 @@ public interface BeanFactory {
|
||||
* Determine the type of the bean with the given name. More specifically,
|
||||
* determine the type of object that {@link #getBean} would return for the given name.
|
||||
* <p>For a {@link FactoryBean}, return the type of object that the FactoryBean creates,
|
||||
* as exposed by {@link FactoryBean#getObjectType()}.
|
||||
* as exposed by {@link FactoryBean#getObjectType()}. This may lead to the initialization
|
||||
* of a previously uninitialized {@code FactoryBean} (see {@link #getType(String, boolean)}).
|
||||
* <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
|
||||
@@ -336,6 +337,27 @@ public interface BeanFactory {
|
||||
@Nullable
|
||||
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
|
||||
|
||||
/**
|
||||
* Determine the type of the bean with the given name. More specifically,
|
||||
* determine the type of object that {@link #getBean} would return for the given name.
|
||||
* <p>For a {@link FactoryBean}, return the type of object that the FactoryBean creates,
|
||||
* as exposed by {@link FactoryBean#getObjectType()}. Depending on the
|
||||
* {@code allowFactoryBeanInit} flag, this may lead to the initialization of a previously
|
||||
* uninitialized {@code FactoryBean} if no early type information is available.
|
||||
* <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 allowFactoryBeanInit whether a {@code FactoryBean} may get initialized
|
||||
* just for the purpose of determining its object type
|
||||
* @return the type of the bean, or {@code null} if not determinable
|
||||
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
|
||||
* @since 5.2
|
||||
* @see #getBean
|
||||
* @see #isTypeMatch
|
||||
*/
|
||||
@Nullable
|
||||
Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
|
||||
|
||||
/**
|
||||
* Return the aliases for the given bean name, if any.
|
||||
* All of those aliases point to the same bean when used in a {@link #getBean} call.
|
||||
|
||||
@@ -502,14 +502,13 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
* {@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
|
||||
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
|
||||
* @since 5.2
|
||||
* @see #getBean
|
||||
* @see #getType
|
||||
*/
|
||||
boolean isTypeMatch(String name, ResolvableType typeToMatch,
|
||||
boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
|
||||
protected boolean isTypeMatch(String name, ResolvableType typeToMatch, boolean allowFactoryBeanInit)
|
||||
throws NoSuchBeanDefinitionException {
|
||||
|
||||
String beanName = transformedBeanName(name);
|
||||
boolean isFactoryDereference = BeanFactoryUtils.isFactoryDereference(name);
|
||||
@@ -657,6 +656,12 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
@Override
|
||||
@Nullable
|
||||
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
|
||||
return getType(name, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
|
||||
String beanName = transformedBeanName(name);
|
||||
|
||||
// Check manually registered singletons.
|
||||
@@ -696,7 +701,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
|
||||
if (!BeanFactoryUtils.isFactoryDereference(name)) {
|
||||
// If it's a FactoryBean, we want to look at what it creates, not at the factory class.
|
||||
return getTypeForFactoryBean(beanName, mbd, true).resolve();
|
||||
return getTypeForFactoryBean(beanName, mbd, allowFactoryBeanInit).resolve();
|
||||
}
|
||||
else {
|
||||
return beanClass;
|
||||
|
||||
@@ -287,6 +287,11 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
|
||||
@Override
|
||||
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
|
||||
return getType(name, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
|
||||
String beanName = BeanFactoryUtils.transformedBeanName(name);
|
||||
|
||||
Object bean = this.beans.get(beanName);
|
||||
|
||||
@@ -1177,6 +1177,13 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
||||
return getBeanFactory().getType(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
|
||||
assertBeanFactoryActive();
|
||||
return getBeanFactory().getType(name, allowFactoryBeanInit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getAliases(String name) {
|
||||
return getBeanFactory().getAliases(name);
|
||||
|
||||
@@ -234,6 +234,12 @@ public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFac
|
||||
@Override
|
||||
@Nullable
|
||||
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
|
||||
return getType(name, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
|
||||
try {
|
||||
return doGetType(name);
|
||||
}
|
||||
|
||||
@@ -221,6 +221,11 @@ class StubWebApplicationContext implements WebApplicationContext {
|
||||
return this.beanFactory.getType(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
|
||||
return this.beanFactory.getType(name, allowFactoryBeanInit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getAliases(String name) {
|
||||
return this.beanFactory.getAliases(name);
|
||||
|
||||
Reference in New Issue
Block a user