Browse Source

Introduce getType variant with allowFactoryBeanInit flag

Closes gh-23374
pull/23580/head
Juergen Hoeller 5 years ago
parent
commit
f26866e4d4
  1. 24
      spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java
  2. 15
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
  3. 5
      spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java
  4. 7
      spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
  5. 6
      spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java
  6. 5
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

24
spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java

@ -323,7 +323,8 @@ public interface BeanFactory { @@ -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 { @@ -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.

15
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

@ -502,14 +502,13 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp @@ -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 @@ -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 @@ -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;

5
spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java

@ -287,6 +287,11 @@ public class StaticListableBeanFactory implements ListableBeanFactory { @@ -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);

7
spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

@ -1177,6 +1177,13 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader @@ -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);

6
spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java

@ -234,6 +234,12 @@ public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFac @@ -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);
}

5
spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

@ -221,6 +221,11 @@ class StubWebApplicationContext implements WebApplicationContext { @@ -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);

Loading…
Cancel
Save