Skip to content

Commit 601e352

Browse files
committed
Polishing
1 parent e37715a commit 601e352

File tree

14 files changed

+272
-221
lines changed

14 files changed

+272
-221
lines changed

spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -194,7 +194,7 @@ public void setWrappedInstance(Object object, @Nullable String nestedPath, @Null
194194
this.wrappedObject = ObjectUtils.unwrapOptional(object);
195195
Assert.notNull(this.wrappedObject, "Target object must not be null");
196196
this.nestedPath = (nestedPath != null ? nestedPath : "");
197-
this.rootObject = (!"".equals(this.nestedPath) ? rootObject : this.wrappedObject);
197+
this.rootObject = (!this.nestedPath.isEmpty() ? rootObject : this.wrappedObject);
198198
this.nestedPropertyAccessors = null;
199199
this.typeConverterDelegate = new TypeConverterDelegate(this, this.wrappedObject);
200200
}
@@ -958,6 +958,7 @@ private PropertyTokenHolder getPropertyNameTokens(String propertyName) {
958958
return tokens;
959959
}
960960

961+
961962
@Override
962963
public String toString() {
963964
StringBuilder sb = new StringBuilder(getClass().getName());
@@ -971,6 +972,9 @@ public String toString() {
971972
}
972973

973974

975+
/**
976+
* A handler for a specific property.
977+
*/
974978
protected abstract static class PropertyHandler {
975979

976980
private final Class<?> propertyType;
@@ -1026,6 +1030,9 @@ public Class<?> getCollectionType(int nestingLevel) {
10261030
}
10271031

10281032

1033+
/**
1034+
* Holder class used to store property tokens.
1035+
*/
10291036
protected static class PropertyTokenHolder {
10301037

10311038
public PropertyTokenHolder(String name) {

spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -243,18 +243,19 @@ <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSin
243243
throws BeansException;
244244

245245
/**
246-
* Find all names of beans whose {@code Class} has the supplied {@link Annotation}
246+
* Find all names of beans which are annotated with the supplied {@link Annotation}
247247
* type, without creating corresponding bean instances yet.
248248
* <p>Note that this method considers objects created by FactoryBeans, which means
249249
* that FactoryBeans will get initialized in order to determine their object type.
250250
* @param annotationType the type of annotation to look for
251251
* @return the names of all matching beans
252252
* @since 4.0
253+
* @see #findAnnotationOnBean
253254
*/
254255
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
255256

256257
/**
257-
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
258+
* Find all beans which are annotated with the supplied {@link Annotation} type,
258259
* returning a Map of bean names with corresponding bean instances.
259260
* <p>Note that this method considers objects created by FactoryBeans, which means
260261
* that FactoryBeans will get initialized in order to determine their object type.
@@ -263,18 +264,21 @@ <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSin
263264
* keys and the corresponding bean instances as values
264265
* @throws BeansException if a bean could not be created
265266
* @since 3.0
267+
* @see #findAnnotationOnBean
266268
*/
267269
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
268270

269271
/**
270-
* Find an {@link Annotation} of {@code annotationType} on the specified
271-
* bean, traversing its interfaces and super classes if no annotation can be
272-
* found on the given class itself.
272+
* Find an {@link Annotation} of {@code annotationType} on the specified bean,
273+
* traversing its interfaces and super classes if no annotation can be found on
274+
* the given class itself.
273275
* @param beanName the name of the bean to look for annotations on
274-
* @param annotationType the annotation class to look for
276+
* @param annotationType the type of annotation to look for
275277
* @return the annotation of the given type if found, or {@code null} otherwise
276278
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
277279
* @since 3.0
280+
* @see #getBeanNamesForAnnotation
281+
* @see #getBeansWithAnnotation
278282
*/
279283
@Nullable
280284
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ public void overrideFrom(BeanDefinition other) {
352352

353353
/**
354354
* Apply the provided default values to this bean.
355-
* @param defaults the defaults to apply
355+
* @param defaults the default settings to apply
356+
* @since 2.5
356357
*/
357358
public void applyDefaults(BeanDefinitionDefaults defaults) {
358359
setLazyInit(defaults.isLazyInit());
@@ -515,6 +516,7 @@ public void setLazyInit(boolean lazyInit) {
515516
/**
516517
* Return whether this bean should be lazily initialized, i.e. not
517518
* eagerly instantiated on startup. Only applicable to a singleton bean.
519+
* @return whether to apply lazy-init semantics ({@code false} by default)
518520
*/
519521
@Override
520522
public boolean isLazyInit() {
@@ -523,8 +525,9 @@ public boolean isLazyInit() {
523525

524526
/**
525527
* Set the autowire mode. This determines whether any automagical detection
526-
* and setting of bean references will happen. Default is AUTOWIRE_NO,
527-
* which means there's no autowire.
528+
* and setting of bean references will happen. Default is AUTOWIRE_NO
529+
* which means there won't be convention-based autowiring by name or type
530+
* (however, there may still be explicit annotation-driven autowiring).
528531
* @param autowireMode the autowire mode to set.
529532
* Must be one of the constants defined in this class.
530533
* @see #AUTOWIRE_NO

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

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,60 +23,102 @@
2323
* A simple holder for {@code BeanDefinition} property defaults.
2424
*
2525
* @author Mark Fisher
26+
* @author Juergen Hoeller
2627
* @since 2.5
2728
*/
2829
public class BeanDefinitionDefaults {
2930

3031
private boolean lazyInit;
3132

32-
private int dependencyCheck = AbstractBeanDefinition.DEPENDENCY_CHECK_NONE;
33-
3433
private int autowireMode = AbstractBeanDefinition.AUTOWIRE_NO;
3534

35+
private int dependencyCheck = AbstractBeanDefinition.DEPENDENCY_CHECK_NONE;
36+
3637
@Nullable
3738
private String initMethodName;
3839

3940
@Nullable
4041
private String destroyMethodName;
4142

4243

44+
/**
45+
* Set whether beans should be lazily initialized by default.
46+
* <p>If {@code false}, the bean will get instantiated on startup by bean
47+
* factories that perform eager initialization of singletons.
48+
*/
4349
public void setLazyInit(boolean lazyInit) {
4450
this.lazyInit = lazyInit;
4551
}
4652

53+
/**
54+
* Return whether beans should be lazily initialized by default, i.e. not
55+
* eagerly instantiated on startup. Only applicable to singleton beans.
56+
* @return whether to apply lazy-init semantics ({@code false} by default)
57+
*/
4758
public boolean isLazyInit() {
4859
return this.lazyInit;
4960
}
5061

51-
public void setDependencyCheck(int dependencyCheck) {
52-
this.dependencyCheck = dependencyCheck;
53-
}
54-
55-
public int getDependencyCheck() {
56-
return this.dependencyCheck;
57-
}
58-
62+
/**
63+
* Set the autowire mode. This determines whether any automagical detection
64+
* and setting of bean references will happen. Default is AUTOWIRE_NO
65+
* which means there won't be convention-based autowiring by name or type
66+
* (however, there may still be explicit annotation-driven autowiring).
67+
* @param autowireMode the autowire mode to set.
68+
* Must be one of the constants defined in {@link AbstractBeanDefinition}.
69+
*/
5970
public void setAutowireMode(int autowireMode) {
6071
this.autowireMode = autowireMode;
6172
}
6273

74+
/**
75+
* Return the default autowire mode.
76+
*/
6377
public int getAutowireMode() {
6478
return this.autowireMode;
6579
}
6680

81+
/**
82+
* Set the dependency check code.
83+
* @param dependencyCheck the code to set.
84+
* Must be one of the constants defined in {@link AbstractBeanDefinition}.
85+
*/
86+
public void setDependencyCheck(int dependencyCheck) {
87+
this.dependencyCheck = dependencyCheck;
88+
}
89+
90+
/**
91+
* Return the default dependency check code.
92+
*/
93+
public int getDependencyCheck() {
94+
return this.dependencyCheck;
95+
}
96+
97+
/**
98+
* Set the name of the default initializer method.
99+
*/
67100
public void setInitMethodName(@Nullable String initMethodName) {
68101
this.initMethodName = (StringUtils.hasText(initMethodName) ? initMethodName : null);
69102
}
70103

104+
/**
105+
* Return the name of the default initializer method.
106+
*/
71107
@Nullable
72108
public String getInitMethodName() {
73109
return this.initMethodName;
74110
}
75111

112+
/**
113+
* Set the name of the default destroy method.
114+
*/
76115
public void setDestroyMethodName(@Nullable String destroyMethodName) {
77116
this.destroyMethodName = (StringUtils.hasText(destroyMethodName) ? destroyMethodName : null);
78117
}
79118

119+
/**
120+
* Return the name of the default destroy method.
121+
*/
80122
@Nullable
81123
public String getDestroyMethodName() {
82124
return this.destroyMethodName;

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,6 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
563563
return result;
564564
}
565565

566-
/**
567-
* Find a {@link Annotation} of {@code annotationType} on the specified
568-
* bean, traversing its interfaces and super classes if no annotation can be
569-
* found on the given class itself, as well as checking its raw bean class
570-
* if not found on the exposed bean reference (e.g. in case of a proxy).
571-
*/
572566
@Override
573567
@Nullable
574568
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
@@ -580,14 +574,12 @@ public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> a
580574
ann = AnnotationUtils.findAnnotation(beanType, annotationType);
581575
}
582576
if (ann == null && containsBeanDefinition(beanName)) {
583-
BeanDefinition bd = getMergedBeanDefinition(beanName);
584-
if (bd instanceof AbstractBeanDefinition) {
585-
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
586-
if (abd.hasBeanClass()) {
587-
Class<?> beanClass = abd.getBeanClass();
588-
if (beanClass != beanType) {
589-
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
590-
}
577+
// Check raw bean class, e.g. in case of a proxy.
578+
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
579+
if (bd.hasBeanClass()) {
580+
Class<?> beanClass = bd.getBeanClass();
581+
if (beanClass != beanType) {
582+
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
591583
}
592584
}
593585
}
@@ -1793,10 +1785,11 @@ public FactoryAwareOrderSourceProvider(Map<Object, String> instancesToBeanNames)
17931785
@Override
17941786
@Nullable
17951787
public Object getOrderSource(Object obj) {
1796-
RootBeanDefinition beanDefinition = getRootBeanDefinition(this.instancesToBeanNames.get(obj));
1797-
if (beanDefinition == null) {
1788+
String beanName = this.instancesToBeanNames.get(obj);
1789+
if (beanName == null || !containsBeanDefinition(beanName)) {
17981790
return null;
17991791
}
1792+
RootBeanDefinition beanDefinition = getMergedLocalBeanDefinition(beanName);
18001793
List<Object> sources = new ArrayList<>(2);
18011794
Method factoryMethod = beanDefinition.getResolvedFactoryMethod();
18021795
if (factoryMethod != null) {
@@ -1808,17 +1801,6 @@ public Object getOrderSource(Object obj) {
18081801
}
18091802
return sources.toArray();
18101803
}
1811-
1812-
@Nullable
1813-
private RootBeanDefinition getRootBeanDefinition(@Nullable String beanName) {
1814-
if (beanName != null && containsBeanDefinition(beanName)) {
1815-
BeanDefinition bd = getMergedBeanDefinition(beanName);
1816-
if (bd instanceof RootBeanDefinition) {
1817-
return (RootBeanDefinition) bd;
1818-
}
1819-
}
1820-
return null;
1821-
}
18221804
}
18231805

18241806
}

spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void initDefaults(Element root, @Nullable BeanDefinitionParserDelegate pa
312312
/**
313313
* Populate the given DocumentDefaultsDefinition instance with the default lazy-init,
314314
* autowire, dependency check settings, init-method, destroy-method and merge settings.
315-
* Support nested 'beans' element use cases by falling back to <literal>parentDefaults</literal>
315+
* Support nested 'beans' element use cases by falling back to {@code parentDefaults}
316316
* in case the defaults are not explicitly set locally.
317317
* @param defaults the defaults to populate
318318
* @param parentDefaults the parent BeanDefinitionParserDelegate (if any) defaults to fall back to
@@ -377,7 +377,7 @@ public DocumentDefaultsDefinition getDefaults() {
377377
*/
378378
public BeanDefinitionDefaults getBeanDefinitionDefaults() {
379379
BeanDefinitionDefaults bdd = new BeanDefinitionDefaults();
380-
bdd.setLazyInit("TRUE".equalsIgnoreCase(this.defaults.getLazyInit()));
380+
bdd.setLazyInit(TRUE_VALUE.equalsIgnoreCase(this.defaults.getLazyInit()));
381381
bdd.setAutowireMode(getAutowireMode(DEFAULT_VALUE));
382382
bdd.setInitMethodName(this.defaults.getInitMethod());
383383
bdd.setDestroyMethodName(this.defaults.getDestroyMethod());

0 commit comments

Comments
 (0)