Skip to content

Commit f26866e

Browse files
committed
Introduce getType variant with allowFactoryBeanInit flag
Closes gh-23374
1 parent eb65777 commit f26866e

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ public interface BeanFactory {
323323
* Determine the type of the bean with the given name. More specifically,
324324
* determine the type of object that {@link #getBean} would return for the given name.
325325
* <p>For a {@link FactoryBean}, return the type of object that the FactoryBean creates,
326-
* as exposed by {@link FactoryBean#getObjectType()}.
326+
* as exposed by {@link FactoryBean#getObjectType()}. This may lead to the initialization
327+
* of a previously uninitialized {@code FactoryBean} (see {@link #getType(String, boolean)}).
327328
* <p>Translates aliases back to the corresponding canonical bean name.
328329
* Will ask the parent factory if the bean cannot be found in this factory instance.
329330
* @param name the name of the bean to query
@@ -336,6 +337,27 @@ public interface BeanFactory {
336337
@Nullable
337338
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
338339

340+
/**
341+
* Determine the type of the bean with the given name. More specifically,
342+
* determine the type of object that {@link #getBean} would return for the given name.
343+
* <p>For a {@link FactoryBean}, return the type of object that the FactoryBean creates,
344+
* as exposed by {@link FactoryBean#getObjectType()}. Depending on the
345+
* {@code allowFactoryBeanInit} flag, this may lead to the initialization of a previously
346+
* uninitialized {@code FactoryBean} if no early type information is available.
347+
* <p>Translates aliases back to the corresponding canonical bean name.
348+
* Will ask the parent factory if the bean cannot be found in this factory instance.
349+
* @param name the name of the bean to query
350+
* @param allowFactoryBeanInit whether a {@code FactoryBean} may get initialized
351+
* just for the purpose of determining its object type
352+
* @return the type of the bean, or {@code null} if not determinable
353+
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
354+
* @since 5.2
355+
* @see #getBean
356+
* @see #isTypeMatch
357+
*/
358+
@Nullable
359+
Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
360+
339361
/**
340362
* Return the aliases for the given bean name, if any.
341363
* All of those aliases point to the same bean when used in a {@link #getBean} call.

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,13 @@ public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuc
502502
* {@code ResolvableType})
503503
* @return {@code true} if the bean type matches, {@code false} if it
504504
* doesn't match or cannot be determined yet
505-
* @throws NoSuchBeanDefinitionException if there is no bean with the given
506-
* name
505+
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
507506
* @since 5.2
508507
* @see #getBean
509508
* @see #getType
510509
*/
511-
boolean isTypeMatch(String name, ResolvableType typeToMatch,
512-
boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
510+
protected boolean isTypeMatch(String name, ResolvableType typeToMatch, boolean allowFactoryBeanInit)
511+
throws NoSuchBeanDefinitionException {
513512

514513
String beanName = transformedBeanName(name);
515514
boolean isFactoryDereference = BeanFactoryUtils.isFactoryDereference(name);
@@ -657,6 +656,12 @@ public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanD
657656
@Override
658657
@Nullable
659658
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
659+
return getType(name, true);
660+
}
661+
662+
@Override
663+
@Nullable
664+
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
660665
String beanName = transformedBeanName(name);
661666

662667
// Check manually registered singletons.
@@ -696,7 +701,7 @@ public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
696701
if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
697702
if (!BeanFactoryUtils.isFactoryDereference(name)) {
698703
// If it's a FactoryBean, we want to look at what it creates, not at the factory class.
699-
return getTypeForFactoryBean(beanName, mbd, true).resolve();
704+
return getTypeForFactoryBean(beanName, mbd, allowFactoryBeanInit).resolve();
700705
}
701706
else {
702707
return beanClass;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ public boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws N
287287

288288
@Override
289289
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
290+
return getType(name, true);
291+
}
292+
293+
@Override
294+
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
290295
String beanName = BeanFactoryUtils.transformedBeanName(name);
291296

292297
Object bean = this.beans.get(beanName);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,13 @@ public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
11771177
return getBeanFactory().getType(name);
11781178
}
11791179

1180+
@Override
1181+
@Nullable
1182+
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
1183+
assertBeanFactoryActive();
1184+
return getBeanFactory().getType(name, allowFactoryBeanInit);
1185+
}
1186+
11801187
@Override
11811188
public String[] getAliases(String name) {
11821189
return getBeanFactory().getAliases(name);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ public boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws N
234234
@Override
235235
@Nullable
236236
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
237+
return getType(name, true);
238+
}
239+
240+
@Override
241+
@Nullable
242+
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
237243
try {
238244
return doGetType(name);
239245
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
221221
return this.beanFactory.getType(name);
222222
}
223223

224+
@Override
225+
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
226+
return this.beanFactory.getType(name, allowFactoryBeanInit);
227+
}
228+
224229
@Override
225230
public String[] getAliases(String name) {
226231
return this.beanFactory.getAliases(name);

0 commit comments

Comments
 (0)