Polishing

This commit is contained in:
Juergen Hoeller
2018-09-19 22:51:35 +02:00
parent c0b0ee6db7
commit f5e6c707ae
12 changed files with 126 additions and 150 deletions

View File

@@ -24,7 +24,6 @@ import groovy.lang.GroovyObjectSupport;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
@@ -182,16 +181,16 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
AbstractBeanDefinition bd = getBeanDefinition();
if (AUTOWIRE.equals(property)) {
if ("byName".equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
}
else if ("byType".equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
}
else if ("constructor".equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
}
else if (Boolean.TRUE.equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
}
}
// constructorArgs
@@ -211,8 +210,9 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
}
// factoryMethod
else if (FACTORY_METHOD.equals(property)) {
if (newValue != null)
if (newValue != null) {
bd.setFactoryMethodName(newValue.toString());
}
}
// initMethod
else if (INIT_METHOD.equals(property)) {

View File

@@ -154,10 +154,10 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
*/
private final NamedThreadLocal<String> currentlyCreatedBean = new NamedThreadLocal<>("Currently created bean");
/** Cache of unfinished FactoryBean instances: FactoryBean name --> BeanWrapper */
/** Cache of unfinished FactoryBean instances: FactoryBean name to BeanWrapper */
private final ConcurrentMap<String, BeanWrapper> factoryBeanInstanceCache = new ConcurrentHashMap<>(16);
/** Cache of filtered PropertyDescriptors: bean Class -> PropertyDescriptor array */
/** Cache of filtered PropertyDescriptors: bean Class to PropertyDescriptor array */
private final ConcurrentMap<Class<?>, PropertyDescriptor[]> filteredPropertyDescriptorsCache =
new ConcurrentHashMap<>(256);
@@ -1116,10 +1116,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
}
// Need to determine the constructor...
// Candidate constructors for autowiring?
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
@@ -1309,25 +1308,21 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// Add property values based on autowire by name if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME) {
autowireByName(beanName, mbd, bw, newPvs);
}
// Add property values based on autowire by type if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {
autowireByType(beanName, mbd, bw, newPvs);
}
pvs = newPvs;
}
boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);
if (hasInstAwareBpps || needsDepCheck) {
if (pvs == null) {
@@ -1532,9 +1527,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
for (PropertyDescriptor pd : pds) {
if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) {
boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) ||
(isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
(!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
boolean unsatisfied = (dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_ALL) ||
(isSimple && dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
(!isSimple && dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
if (unsatisfied) {
throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
"Set this property value or disable dependency checking for this bean.");
@@ -1787,7 +1782,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
if (initMethod == null) {
if (mbd.isEnforceInitMethod()) {
throw new BeanDefinitionValidationException("Couldn't find an init method named '" +
throw new BeanDefinitionValidationException("Could not find an init method named '" +
initMethodName + "' on bean with name '" + beanName + "'");
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -69,9 +69,7 @@ public class BeanDefinitionBuilder {
* @param instanceSupplier a callback for creating an instance of the bean
* @since 5.0
*/
public static <T> BeanDefinitionBuilder genericBeanDefinition(
@Nullable Class<T> beanClass, Supplier<T> instanceSupplier) {
public static <T> BeanDefinitionBuilder genericBeanDefinition(Class<T> beanClass, Supplier<T> instanceSupplier) {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder(new GenericBeanDefinition());
builder.beanDefinition.setBeanClass(beanClass);
builder.beanDefinition.setInstanceSupplier(instanceSupplier);
@@ -275,7 +273,7 @@ public class BeanDefinitionBuilder {
* Set the autowire mode for this definition.
*/
public BeanDefinitionBuilder setAutowireMode(int autowireMode) {
beanDefinition.setAutowireMode(autowireMode);
this.beanDefinition.setAutowireMode(autowireMode);
return this;
}
@@ -283,7 +281,7 @@ public class BeanDefinitionBuilder {
* Set the depency check mode for this definition.
*/
public BeanDefinitionBuilder setDependencyCheck(int dependencyCheck) {
beanDefinition.setDependencyCheck(dependencyCheck);
this.beanDefinition.setDependencyCheck(dependencyCheck);
return this;
}
@@ -316,7 +314,7 @@ public class BeanDefinitionBuilder {
*/
public BeanDefinitionBuilder applyCustomizers(BeanDefinitionCustomizer... customizers) {
for (BeanDefinitionCustomizer customizer : customizers) {
customizer.customize(beanDefinition);
customizer.customize(this.beanDefinition);
}
return this;
}

View File

@@ -44,6 +44,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
import org.springframework.beans.factory.config.DependencyDescriptor;
@@ -140,7 +141,7 @@ class ConstructorResolver {
if (constructorToUse == null) {
// Need to resolve the constructor.
boolean autowiring = (chosenCtors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
ConstructorArgumentValues resolvedValues = null;
int minNrOfArgs;
@@ -427,7 +428,7 @@ class ConstructorResolver {
AutowireUtils.sortFactoryMethods(candidates);
ConstructorArgumentValues resolvedValues = null;
boolean autowiring = (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
boolean autowiring = (mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
int minTypeDiffWeight = Integer.MAX_VALUE;
Set<Method> ambiguousFactoryMethods = null;

View File

@@ -115,7 +115,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
this.destroyMethod = determineDestroyMethod(destroyMethodName);
if (this.destroyMethod == null) {
if (beanDefinition.isEnforceDestroyMethod()) {
throw new BeanDefinitionValidationException("Couldn't find a destroy method named '" +
throw new BeanDefinitionValidationException("Could not find a destroy method named '" +
destroyMethodName + "' on bean with name '" + beanName + "'");
}
}