Refactor Improve performance by inducing hot path from the point of view of the jit compilor
Signed-off-by: KNU-K <knukang334@gmail.com>
This commit is contained in:
parent
015edb33cd
commit
f7f164f8ee
|
@ -615,26 +615,96 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
|
|
||||||
// Check all bean definitions.
|
// Check all bean definitions.
|
||||||
|
processBeanDefinitions(result, type, includeNonSingletons, allowEagerInit);
|
||||||
|
|
||||||
|
// Check manually registered singletons too.
|
||||||
|
processManualSingletons(result, type, includeNonSingletons);
|
||||||
|
|
||||||
|
return StringUtils.toStringArray(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processBeanDefinitions(List<String> result, ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||||
for (String beanName : this.beanDefinitionNames) {
|
for (String beanName : this.beanDefinitionNames) {
|
||||||
// Only consider bean as eligible if the bean name is not defined as alias for some other bean.
|
// Only consider bean as eligible if the bean name is not defined as alias for some other bean.
|
||||||
if (!isAlias(beanName)) {
|
if (isAlias(beanName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
RootBeanDefinition mbd;
|
||||||
try {
|
try {
|
||||||
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
|
mbd = getMergedLocalBeanDefinition(beanName);
|
||||||
// Only check bean definition if it is complete.
|
}
|
||||||
if (!mbd.isAbstract() && (allowEagerInit ||
|
catch (CannotLoadBeanClassException | BeanDefinitionStoreException ex) {
|
||||||
(mbd.hasBeanClass() || !mbd.isLazyInit() || isAllowEagerClassLoading()) &&
|
if (allowEagerInit) {
|
||||||
!requiresEagerInitForType(mbd.getFactoryBeanName()))) {
|
throw ex;
|
||||||
boolean isFactoryBean = isFactoryBean(beanName, mbd);
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
|
||||||
boolean matchFound = false;
|
|
||||||
|
// 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 allowFactoryBeanInit = (allowEagerInit || containsSingleton(beanName));
|
||||||
boolean isNonLazyDecorated = (dbd != null && !mbd.isLazyInit());
|
boolean isNonLazyDecorated = (dbd != null && !isLazyInit);
|
||||||
|
|
||||||
if (!isFactoryBean) {
|
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)) {
|
if (includeNonSingletons || isSingleton(beanName, mbd, dbd)) {
|
||||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
|
return isTypeMatch(beanName, type, allowFactoryBeanInit);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
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) {
|
if (includeNonSingletons || isNonLazyDecorated) {
|
||||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
|
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
|
||||||
}
|
}
|
||||||
|
@ -644,52 +714,26 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit) &&
|
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit) &&
|
||||||
isSingleton(beanName, mbd, dbd);
|
isSingleton(beanName, mbd, dbd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!matchFound) {
|
if (!matchFound) {
|
||||||
// In case of FactoryBean, try to match FactoryBean instance itself next.
|
// In case of FactoryBean, try to match FactoryBean instance itself next.
|
||||||
beanName = FACTORY_BEAN_PREFIX + beanName;
|
String factoryBeanName = FACTORY_BEAN_PREFIX + beanName;
|
||||||
if (includeNonSingletons || isSingleton(beanName, mbd, dbd)) {
|
if (includeNonSingletons || isSingleton(factoryBeanName, mbd, dbd)) {
|
||||||
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
|
matchFound = isTypeMatch(factoryBeanName, type, allowFactoryBeanInit);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (matchFound) {
|
if (matchFound) {
|
||||||
result.add(beanName);
|
result.add(factoryBeanName);
|
||||||
}
|
return false; // Don't add original beanName
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (CannotLoadBeanClassException | BeanDefinitionStoreException ex) {
|
|
||||||
if (allowEagerInit) {
|
|
||||||
throw 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);
|
|
||||||
}
|
|
||||||
catch (NoSuchBeanDefinitionException ex) {
|
|
||||||
// Bean definition got removed while we were iterating -> ignore.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check manually registered singletons too.
|
return matchFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processManualSingletons(List<String> result, ResolvableType type, boolean includeNonSingletons) {
|
||||||
for (String beanName : this.manualSingletonNames) {
|
for (String beanName : this.manualSingletonNames) {
|
||||||
try {
|
try {
|
||||||
// In case of FactoryBean, match object created by FactoryBean.
|
if (processManualSingleton(result, beanName, type, includeNonSingletons)) {
|
||||||
if (isFactoryBean(beanName)) {
|
|
||||||
if ((includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type)) {
|
|
||||||
result.add(beanName);
|
|
||||||
// Match found for this bean: do not match FactoryBean itself anymore.
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// In case of FactoryBean, try to match FactoryBean itself next.
|
|
||||||
beanName = FACTORY_BEAN_PREFIX + beanName;
|
|
||||||
}
|
|
||||||
// Match raw bean instance (might be raw FactoryBean).
|
|
||||||
if (isTypeMatch(beanName, type)) {
|
|
||||||
result.add(beanName);
|
result.add(beanName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -699,8 +743,24 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
"Failed to check manually registered singleton with name '%s'", beanName), ex);
|
"Failed to check manually registered singleton with name '%s'", beanName), ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return StringUtils.toStringArray(result);
|
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) {
|
private boolean isSingleton(String beanName, RootBeanDefinition mbd, @Nullable BeanDefinitionHolder dbd) {
|
||||||
|
|
Loading…
Reference in New Issue