Polishing

This commit is contained in:
Juergen Hoeller
2019-04-03 17:54:33 +02:00
parent c5802e98e9
commit f6b2c0f780
14 changed files with 276 additions and 227 deletions

View File

@@ -972,6 +972,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
return tokens;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getName());
@@ -985,6 +986,9 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
}
/**
* A handler for a specific property.
*/
protected abstract static class PropertyHandler {
private final Class<?> propertyType;
@@ -1035,6 +1039,9 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
}
/**
* Holder class used to store property tokens.
*/
protected static class PropertyTokenHolder {
public String canonicalName;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -242,18 +242,19 @@ public interface ListableBeanFactory extends BeanFactory {
throws BeansException;
/**
* Find all names of beans whose {@code Class} has the supplied {@link Annotation}
* Find all names of beans which are annotated with the supplied {@link Annotation}
* type, without creating corresponding bean instances yet.
* <p>Note that this method considers objects created by FactoryBeans, which means
* that FactoryBeans will get initialized in order to determine their object type.
* @param annotationType the type of annotation to look for
* @return the names of all matching beans
* @since 4.0
* @see #findAnnotationOnBean
*/
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
/**
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
* Find all beans which are annotated with the supplied {@link Annotation} type,
* returning a Map of bean names with corresponding bean instances.
* <p>Note that this method considers objects created by FactoryBeans, which means
* that FactoryBeans will get initialized in order to determine their object type.
@@ -262,18 +263,21 @@ public interface ListableBeanFactory extends BeanFactory {
* keys and the corresponding bean instances as values
* @throws BeansException if a bean could not be created
* @since 3.0
* @see #findAnnotationOnBean
*/
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
/**
* Find an {@link Annotation} of {@code annotationType} on the specified
* bean, traversing its interfaces and super classes if no annotation can be
* found on the given class itself.
* Find an {@link Annotation} of {@code annotationType} on the specified bean,
* traversing its interfaces and super classes if no annotation can be found on
* the given class itself.
* @param beanName the name of the bean to look for annotations on
* @param annotationType the annotation class to look for
* @param annotationType the type of annotation to look for
* @return the annotation of the given type if found, or {@code null} otherwise
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
* @since 3.0
* @see #getBeanNamesForAnnotation
* @see #getBeansWithAnnotation
*/
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException;

View File

@@ -318,7 +318,8 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/**
* Apply the provided default values to this bean.
* @param defaults the defaults to apply
* @param defaults the default settings to apply
* @since 2.5
*/
public void applyDefaults(BeanDefinitionDefaults defaults) {
setLazyInit(defaults.isLazyInit());
@@ -478,6 +479,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/**
* Return whether this bean should be lazily initialized, i.e. not
* eagerly instantiated on startup. Only applicable to a singleton bean.
* @return whether to apply lazy-init semantics ({@code false} by default)
*/
@Override
public boolean isLazyInit() {
@@ -486,8 +488,9 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/**
* Set the autowire mode. This determines whether any automagical detection
* and setting of bean references will happen. Default is AUTOWIRE_NO,
* which means there's no autowire.
* and setting of bean references will happen. Default is AUTOWIRE_NO
* which means there won't be convention-based autowiring by name or type
* (however, there may still be explicit annotation-driven autowiring).
* @param autowireMode the autowire mode to set.
* Must be one of the constants defined in this class.
* @see #AUTOWIRE_NO

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -22,57 +22,99 @@ import org.springframework.util.StringUtils;
* A simple holder for {@code BeanDefinition} property defaults.
*
* @author Mark Fisher
* @author Juergen Hoeller
* @since 2.5
*/
public class BeanDefinitionDefaults {
private boolean lazyInit;
private int dependencyCheck = AbstractBeanDefinition.DEPENDENCY_CHECK_NONE;
private int autowireMode = AbstractBeanDefinition.AUTOWIRE_NO;
private int dependencyCheck = AbstractBeanDefinition.DEPENDENCY_CHECK_NONE;
private String initMethodName;
private String destroyMethodName;
/**
* Set whether beans should be lazily initialized by default.
* <p>If {@code false}, the bean will get instantiated on startup by bean
* factories that perform eager initialization of singletons.
*/
public void setLazyInit(boolean lazyInit) {
this.lazyInit = lazyInit;
}
/**
* Return whether beans should be lazily initialized by default, i.e. not
* eagerly instantiated on startup. Only applicable to singleton beans.
* @return whether to apply lazy-init semantics ({@code false} by default)
*/
public boolean isLazyInit() {
return this.lazyInit;
}
public void setDependencyCheck(int dependencyCheck) {
this.dependencyCheck = dependencyCheck;
}
public int getDependencyCheck() {
return this.dependencyCheck;
}
/**
* Set the autowire mode. This determines whether any automagical detection
* and setting of bean references will happen. Default is AUTOWIRE_NO
* which means there won't be convention-based autowiring by name or type
* (however, there may still be explicit annotation-driven autowiring).
* @param autowireMode the autowire mode to set.
* Must be one of the constants defined in {@link AbstractBeanDefinition}.
*/
public void setAutowireMode(int autowireMode) {
this.autowireMode = autowireMode;
}
/**
* Return the default autowire mode.
*/
public int getAutowireMode() {
return this.autowireMode;
}
/**
* Set the dependency check code.
* @param dependencyCheck the code to set.
* Must be one of the constants defined in {@link AbstractBeanDefinition}.
*/
public void setDependencyCheck(int dependencyCheck) {
this.dependencyCheck = dependencyCheck;
}
/**
* Return the default dependency check code.
*/
public int getDependencyCheck() {
return this.dependencyCheck;
}
/**
* Set the name of the default initializer method.
*/
public void setInitMethodName(String initMethodName) {
this.initMethodName = (StringUtils.hasText(initMethodName) ? initMethodName : null);
}
/**
* Return the name of the default initializer method.
*/
public String getInitMethodName() {
return this.initMethodName;
}
/**
* Set the name of the default destroy method.
*/
public void setDestroyMethodName(String destroyMethodName) {
this.destroyMethodName = (StringUtils.hasText(destroyMethodName) ? destroyMethodName : null);
}
/**
* Return the name of the default destroy method.
*/
public String getDestroyMethodName() {
return this.destroyMethodName;
}

View File

@@ -566,12 +566,6 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return result;
}
/**
* Find a {@link Annotation} of {@code annotationType} on the specified
* bean, traversing its interfaces and super classes if no annotation can be
* found on the given class itself, as well as checking its raw bean class
* if not found on the exposed bean reference (e.g. in case of a proxy).
*/
@Override
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException {
@@ -582,14 +576,12 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
ann = AnnotationUtils.findAnnotation(beanType, annotationType);
}
if (ann == null && containsBeanDefinition(beanName)) {
BeanDefinition bd = getMergedBeanDefinition(beanName);
if (bd instanceof AbstractBeanDefinition) {
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
if (abd.hasBeanClass()) {
Class<?> beanClass = abd.getBeanClass();
if (beanClass != beanType) {
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
}
// Check raw bean class, e.g. in case of a proxy.
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (bd.hasBeanClass()) {
Class<?> beanClass = bd.getBeanClass();
if (beanClass != beanType) {
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
}
}
}
@@ -1756,10 +1748,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
@Override
public Object getOrderSource(Object obj) {
RootBeanDefinition beanDefinition = getRootBeanDefinition(this.instancesToBeanNames.get(obj));
if (beanDefinition == null) {
String beanName = this.instancesToBeanNames.get(obj);
if (beanName == null || !containsBeanDefinition(beanName)) {
return null;
}
RootBeanDefinition beanDefinition = getMergedLocalBeanDefinition(beanName);
List<Object> sources = new ArrayList<Object>(2);
Method factoryMethod = beanDefinition.getResolvedFactoryMethod();
if (factoryMethod != null) {
@@ -1771,16 +1764,6 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
return sources.toArray();
}
private RootBeanDefinition getRootBeanDefinition(String beanName) {
if (beanName != null && containsBeanDefinition(beanName)) {
BeanDefinition bd = getMergedBeanDefinition(beanName);
if (bd instanceof RootBeanDefinition) {
return (RootBeanDefinition) bd;
}
}
return null;
}
}
}

View File

@@ -332,7 +332,7 @@ public class BeanDefinitionParserDelegate {
/**
* Populate the given DocumentDefaultsDefinition instance with the default lazy-init,
* autowire, dependency check settings, init-method, destroy-method and merge settings.
* Support nested 'beans' element use cases by falling back to <literal>parentDefaults</literal>
* Support nested 'beans' element use cases by falling back to {@code parentDefaults}
* in case the defaults are not explicitly set locally.
* @param defaults the defaults to populate
* @param parentDefaults the parent BeanDefinitionParserDelegate (if any) defaults to fall back to
@@ -401,9 +401,9 @@ public class BeanDefinitionParserDelegate {
*/
public BeanDefinitionDefaults getBeanDefinitionDefaults() {
BeanDefinitionDefaults bdd = new BeanDefinitionDefaults();
bdd.setLazyInit("TRUE".equalsIgnoreCase(this.defaults.getLazyInit()));
bdd.setDependencyCheck(getDependencyCheck(DEFAULT_VALUE));
bdd.setLazyInit(TRUE_VALUE.equalsIgnoreCase(this.defaults.getLazyInit()));
bdd.setAutowireMode(getAutowireMode(DEFAULT_VALUE));
bdd.setDependencyCheck(getDependencyCheck(DEFAULT_VALUE));
bdd.setInitMethodName(this.defaults.getInitMethod());
bdd.setDestroyMethodName(this.defaults.getDestroyMethod());
return bdd;