refactor: Simplify bean processing logic in DefaultListableBeanFactory
Signed-off-by: KNU-K <knukang334@gmail.com>
This commit is contained in:
parent
cd81381939
commit
5335ecceb4
|
@ -614,10 +614,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
|||
private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
// Check all bean definitions.
|
||||
processBeanDefinitions(result, type, includeNonSingletons, allowEagerInit);
|
||||
|
||||
// Check manually registered singletons too.
|
||||
processManualSingletons(result, type, includeNonSingletons);
|
||||
|
||||
return StringUtils.toStringArray(result);
|
||||
|
@ -625,145 +622,95 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
|||
|
||||
private void processBeanDefinitions(List<String> result, ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||
for (String beanName : this.beanDefinitionNames) {
|
||||
// Only consider bean as eligible if the bean name is not defined as alias for some other bean.
|
||||
if (isAlias(beanName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
RootBeanDefinition mbd;
|
||||
try {
|
||||
mbd = getMergedLocalBeanDefinition(beanName);
|
||||
}
|
||||
catch (CannotLoadBeanClassException | BeanDefinitionStoreException ex) {
|
||||
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
|
||||
|
||||
// cache merged bean definition
|
||||
boolean isAbstract = mbd.isAbstract();
|
||||
boolean hasBeanClass = mbd.hasBeanClass();
|
||||
boolean isLazyInit = mbd.isLazyInit();
|
||||
String factoryBeanName = mbd.getFactoryBeanName();
|
||||
BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
|
||||
|
||||
if (isAbstract || (!allowEagerInit &&
|
||||
((!hasBeanClass && isLazyInit && !isAllowEagerClassLoading()) ||
|
||||
requiresEagerInitForType(factoryBeanName)))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean isFactoryBean = isFactoryBean(beanName, mbd);
|
||||
boolean matchFound = false;
|
||||
boolean allowFactoryBeanInit = (allowEagerInit || containsSingleton(beanName));
|
||||
boolean isNonLazyDecorated = (dbd != null && !isLazyInit);
|
||||
|
||||
if (!isFactoryBean && (includeNonSingletons || isSingleton(beanName, mbd, dbd))) {
|
||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
|
||||
}
|
||||
else {
|
||||
if (includeNonSingletons || isNonLazyDecorated) {
|
||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
|
||||
}
|
||||
else if (allowFactoryBeanInit) {
|
||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit) &&
|
||||
isSingleton(beanName, mbd, dbd);
|
||||
}
|
||||
|
||||
if (!matchFound) {
|
||||
String factoryBeanFullName = FACTORY_BEAN_PREFIX + beanName;
|
||||
if (includeNonSingletons || isSingleton(factoryBeanFullName, mbd, dbd)) {
|
||||
matchFound = isTypeMatch(factoryBeanFullName, type, allowFactoryBeanInit);
|
||||
}
|
||||
if (matchFound) {
|
||||
result.add(factoryBeanFullName);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchFound) {
|
||||
result.add(beanName);
|
||||
}
|
||||
|
||||
} catch (CannotLoadBeanClassException | BeanDefinitionStoreException ex) {
|
||||
if (allowEagerInit) {
|
||||
throw ex;
|
||||
}
|
||||
handleBeanDefinitionException(beanName, ex);
|
||||
continue;
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
// Bean definition got removed while we were iterating -> ignore.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (processBeanDefinition(result, beanName, mbd, type, includeNonSingletons, allowEagerInit)) {
|
||||
result.add(beanName);
|
||||
LogMessage message = (ex instanceof CannotLoadBeanClassException ?
|
||||
LogMessage.format("Ignoring bean class loading failure for bean '%s'", beanName) :
|
||||
LogMessage.format("Ignoring unresolvable metadata in bean definition '%s'", beanName));
|
||||
logger.trace(message, ex);
|
||||
onSuppressedException(ex);
|
||||
} catch (NoSuchBeanDefinitionException ex) {
|
||||
// Bean definition got removed while we were iterating -> ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleBeanDefinitionException(String beanName, Exception ex) {
|
||||
// Probably a placeholder: let's ignore it for type matching purposes.
|
||||
LogMessage message = (ex instanceof CannotLoadBeanClassException ?
|
||||
LogMessage.format("Ignoring bean class loading failure for bean '%s'", beanName) :
|
||||
LogMessage.format("Ignoring unresolvable metadata in bean definition '%s'", beanName));
|
||||
logger.trace(message, ex);
|
||||
// Register exception, in case the bean was accidentally unresolvable.
|
||||
onSuppressedException(ex);
|
||||
}
|
||||
|
||||
private boolean processBeanDefinition(List<String> result, String beanName, RootBeanDefinition mbd,
|
||||
ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||
// Cache merged bean definition properties to avoid repeated access
|
||||
boolean isAbstract = mbd.isAbstract();
|
||||
boolean hasBeanClass = mbd.hasBeanClass();
|
||||
boolean isLazyInit = mbd.isLazyInit();
|
||||
String factoryBeanName = mbd.getFactoryBeanName();
|
||||
BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
|
||||
|
||||
// Only check bean definition if it is complete.
|
||||
if (isAbstract) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!allowEagerInit &&
|
||||
!(hasBeanClass || !isLazyInit || isAllowEagerClassLoading()) &&
|
||||
requiresEagerInitForType(factoryBeanName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean isFactoryBean = isFactoryBean(beanName, mbd);
|
||||
boolean allowFactoryBeanInit = (allowEagerInit || containsSingleton(beanName));
|
||||
boolean isNonLazyDecorated = (dbd != null && !isLazyInit);
|
||||
|
||||
if (!isFactoryBean) {
|
||||
return processRegularBean(beanName, mbd, dbd, type, includeNonSingletons, allowFactoryBeanInit);
|
||||
}
|
||||
else {
|
||||
return processFactoryBean(result, beanName, mbd, dbd, type, includeNonSingletons, allowFactoryBeanInit, isNonLazyDecorated);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean processRegularBean(String beanName, RootBeanDefinition mbd, @Nullable BeanDefinitionHolder dbd,
|
||||
ResolvableType type, boolean includeNonSingletons, boolean allowFactoryBeanInit) {
|
||||
if (includeNonSingletons || isSingleton(beanName, mbd, dbd)) {
|
||||
return isTypeMatch(beanName, type, allowFactoryBeanInit);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean processFactoryBean(List<String> result, String beanName, RootBeanDefinition mbd, @Nullable BeanDefinitionHolder dbd,
|
||||
ResolvableType type, boolean includeNonSingletons, boolean allowFactoryBeanInit, boolean isNonLazyDecorated) {
|
||||
boolean matchFound = false;
|
||||
|
||||
if (includeNonSingletons || isNonLazyDecorated) {
|
||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
|
||||
}
|
||||
else if (allowFactoryBeanInit) {
|
||||
// Type check before singleton check, avoiding FactoryBean instantiation
|
||||
// for early FactoryBean.isSingleton() calls on non-matching beans.
|
||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit) &&
|
||||
isSingleton(beanName, mbd, dbd);
|
||||
}
|
||||
|
||||
if (!matchFound) {
|
||||
// In case of FactoryBean, try to match FactoryBean instance itself next.
|
||||
String factoryBeanName = FACTORY_BEAN_PREFIX + beanName;
|
||||
if (includeNonSingletons || isSingleton(factoryBeanName, mbd, dbd)) {
|
||||
matchFound = isTypeMatch(factoryBeanName, type, allowFactoryBeanInit);
|
||||
}
|
||||
if (matchFound) {
|
||||
result.add(factoryBeanName);
|
||||
return false; // Don't add original beanName
|
||||
}
|
||||
}
|
||||
|
||||
return matchFound;
|
||||
}
|
||||
|
||||
private void processManualSingletons(List<String> result, ResolvableType type, boolean includeNonSingletons) {
|
||||
for (String beanName : this.manualSingletonNames) {
|
||||
try {
|
||||
if (processManualSingleton(result, beanName, type, includeNonSingletons)) {
|
||||
if (isFactoryBean(beanName)) {
|
||||
if ((includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type)) {
|
||||
result.add(beanName);
|
||||
continue;
|
||||
}
|
||||
beanName = FACTORY_BEAN_PREFIX + beanName;
|
||||
}
|
||||
|
||||
if (isTypeMatch(beanName, type)) {
|
||||
result.add(beanName);
|
||||
}
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
// Shouldn't happen - probably a result of circular reference resolution...
|
||||
} catch (NoSuchBeanDefinitionException ex) {
|
||||
logger.trace(LogMessage.format(
|
||||
"Failed to check manually registered singleton with name '%s'", beanName), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean processManualSingleton(List<String> result, String beanName, ResolvableType type, boolean includeNonSingletons) {
|
||||
// In case of FactoryBean, match object created by FactoryBean.
|
||||
if (isFactoryBean(beanName)) {
|
||||
if ((includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type)) {
|
||||
return true; // Match found for this bean
|
||||
}
|
||||
// In case of FactoryBean, try to match FactoryBean itself next.
|
||||
String factoryBeanName = FACTORY_BEAN_PREFIX + beanName;
|
||||
if (isTypeMatch(factoryBeanName, type)) {
|
||||
result.add(factoryBeanName);
|
||||
}
|
||||
return false; // Don't add original beanName
|
||||
}
|
||||
|
||||
// Match raw bean instance (might be raw FactoryBean).
|
||||
return isTypeMatch(beanName, type);
|
||||
}
|
||||
|
||||
private boolean isSingleton(String beanName, RootBeanDefinition mbd, @Nullable BeanDefinitionHolder dbd) {
|
||||
return (dbd != null ? mbd.isSingleton() : isSingleton(beanName));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue