Browse Source

Polish AbstractAutowireCapableBeanFactory and use instanceof pattern matching

Closes gh-29499
pull/29521/head
wizard 2 years ago committed by Sam Brannen
parent
commit
e9555d4825
  1. 39
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

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

@ -599,8 +599,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
exposedObject = initializeBean(beanName, exposedObject, mbd); exposedObject = initializeBean(beanName, exposedObject, mbd);
} }
catch (Throwable ex) { catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { if (ex instanceof BeanCreationException bce && beanName.equals(bce.getBeanName())) {
throw (BeanCreationException) ex; throw bce;
} }
else { else {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex); throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
@ -862,9 +862,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// declaration without instantiating the containing bean at all. // declaration without instantiating the containing bean at all.
BeanDefinition factoryBeanDefinition = getBeanDefinition(factoryBeanName); BeanDefinition factoryBeanDefinition = getBeanDefinition(factoryBeanName);
Class<?> factoryBeanClass; Class<?> factoryBeanClass;
if (factoryBeanDefinition instanceof AbstractBeanDefinition && if (factoryBeanDefinition instanceof AbstractBeanDefinition abstractBeanDefinition &&
((AbstractBeanDefinition) factoryBeanDefinition).hasBeanClass()) { abstractBeanDefinition.hasBeanClass()) {
factoryBeanClass = ((AbstractBeanDefinition) factoryBeanDefinition).getBeanClass(); factoryBeanClass = abstractBeanDefinition.getBeanClass();
} }
else { else {
RootBeanDefinition fbmbd = getMergedBeanDefinition(factoryBeanName, factoryBeanDefinition); RootBeanDefinition fbmbd = getMergedBeanDefinition(factoryBeanName, factoryBeanDefinition);
@ -1366,7 +1366,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
// state of the bean before properties are set. This can be used, for example, // state of the bean before properties are set. This can be used, for example,
// to support styles of field injection. // to support styles of field injection.
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
if (!mbd.isSynthetic() && hasInstAwareBpps) {
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) { for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
return; return;
@ -1389,10 +1390,6 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
} }
pvs = newPvs; pvs = newPvs;
} }
boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);
if (hasInstAwareBpps) { if (hasInstAwareBpps) {
if (pvs == null) { if (pvs == null) {
pvs = mbd.getPropertyValues(); pvs = mbd.getPropertyValues();
@ -1405,6 +1402,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
pvs = pvsToUse; pvs = pvsToUse;
} }
} }
boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);
if (needsDepCheck) { if (needsDepCheck) {
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
checkDependencies(beanName, mbd, filteredPds, pvs); checkDependencies(beanName, mbd, filteredPds, pvs);
@ -1677,8 +1676,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
} }
deepCopy.add(pv); deepCopy.add(pv);
} }
else if (convertible && originalValue instanceof TypedStringValue && else if (convertible && originalValue instanceof TypedStringValue typedStringValue &&
!((TypedStringValue) originalValue).isDynamic() && !typedStringValue.isDynamic() &&
!(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) { !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
pv.setConvertedValue(convertedValue); pv.setConvertedValue(convertedValue);
deepCopy.add(pv); deepCopy.add(pv);
@ -1709,8 +1708,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
private Object convertForProperty( private Object convertForProperty(
@Nullable Object value, String propertyName, BeanWrapper bw, TypeConverter converter) { @Nullable Object value, String propertyName, BeanWrapper bw, TypeConverter converter) {
if (converter instanceof BeanWrapperImpl) { if (converter instanceof BeanWrapperImpl beanWrapper) {
return ((BeanWrapperImpl) converter).convertForProperty(value, propertyName); return beanWrapper.convertForProperty(value, propertyName);
} }
else { else {
PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName); PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
@ -1761,17 +1760,17 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
private void invokeAwareMethods(String beanName, Object bean) { private void invokeAwareMethods(String beanName, Object bean) {
if (bean instanceof Aware) { if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) { if (bean instanceof BeanNameAware beanNameAware) {
((BeanNameAware) bean).setBeanName(beanName); beanNameAware.setBeanName(beanName);
} }
if (bean instanceof BeanClassLoaderAware) { if (bean instanceof BeanClassLoaderAware beanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader(); ClassLoader bcl = getBeanClassLoader();
if (bcl != null) { if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl); beanClassLoaderAware.setBeanClassLoader(bcl);
} }
} }
if (bean instanceof BeanFactoryAware) { if (bean instanceof BeanFactoryAware beanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this); beanFactoryAware.setBeanFactory(AbstractAutowireCapableBeanFactory.this);
} }
} }
} }

Loading…
Cancel
Save