Skip to content

Commit c2a1a41

Browse files
committed
Polishing
1 parent 0b47c54 commit c2a1a41

File tree

12 files changed

+186
-179
lines changed

12 files changed

+186
-179
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -141,8 +141,7 @@ public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, C
141141

142142
@Override
143143
public PropertyValues postProcessPropertyValues(
144-
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
145-
throws BeansException {
144+
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
146145

147146
if (!this.validatedBeanNames.contains(beanName)) {
148147
if (!shouldSkip(this.beanFactory, beanName)) {

spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -96,14 +96,13 @@ public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
9696
* dependency types - which the factory handles specifically - already filtered out)
9797
* @param bean the bean instance created, but whose properties have not yet been set
9898
* @param beanName the name of the bean
99-
* @return the actual property values to apply to to the given bean
99+
* @return the actual property values to apply to the given bean
100100
* (can be the passed-in PropertyValues instance), or {@code null}
101101
* to skip property population
102102
* @throws org.springframework.beans.BeansException in case of errors
103103
* @see org.springframework.beans.MutablePropertyValues
104104
*/
105105
PropertyValues postProcessPropertyValues(
106-
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
107-
throws BeansException;
106+
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException;
108107

109108
}

spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -66,8 +66,7 @@ public boolean postProcessAfterInstantiation(Object bean, String beanName) throw
6666

6767
@Override
6868
public PropertyValues postProcessPropertyValues(
69-
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
70-
throws BeansException {
69+
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
7170

7271
return pvs;
7372
}

spring-beans/src/main/java/org/springframework/beans/factory/config/Scope.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -68,6 +68,7 @@ public interface Scope {
6868
* @param objectFactory the {@link ObjectFactory} to use to create the scoped
6969
* object if it is not present in the underlying storage mechanism
7070
* @return the desired object (never {@code null})
71+
* @throws IllegalStateException if the underlying scope is not currently active
7172
*/
7273
Object get(String name, ObjectFactory<?> objectFactory);
7374

@@ -84,6 +85,7 @@ public interface Scope {
8485
* removing an object.
8586
* @param name the name of the object to remove
8687
* @return the removed object, or {@code null} if no object was present
88+
* @throws IllegalStateException if the underlying scope is not currently active
8789
* @see #registerDestructionCallback
8890
*/
8991
Object remove(String name);
@@ -100,7 +102,7 @@ public interface Scope {
100102
* at the appropriate time. If such a callback is not supported by the
101103
* underlying runtime environment at all, the callback <i>must be
102104
* ignored and a corresponding warning should be logged</i>.
103-
* <p>Note that 'destruction' refers to to automatic destruction of
105+
* <p>Note that 'destruction' refers to automatic destruction of
104106
* the object as part of the scope's own lifecycle, not to the individual
105107
* scoped object having been explicitly removed by the application.
106108
* If a scoped object gets removed via this facade's {@link #remove(String)}
@@ -112,6 +114,7 @@ public interface Scope {
112114
* so it can safely be executed without an enclosing try-catch block.
113115
* Furthermore, the Runnable will usually be serializable, provided
114116
* that its target object is serializable as well.
117+
* @throws IllegalStateException if the underlying scope is not currently active
115118
* @see org.springframework.beans.factory.DisposableBean
116119
* @see org.springframework.beans.factory.support.AbstractBeanDefinition#getDestroyMethodName()
117120
* @see DestructionAwareBeanPostProcessor
@@ -123,6 +126,7 @@ public interface Scope {
123126
* E.g. the HttpServletRequest object for key "request".
124127
* @param key the contextual key
125128
* @return the corresponding object, or {@code null} if none found
129+
* @throws IllegalStateException if the underlying scope is not currently active
126130
*/
127131
Object resolveContextualObject(String key);
128132

@@ -139,6 +143,7 @@ public interface Scope {
139143
* underlying storage mechanism has no obvious candidate for such an ID.
140144
* @return the conversation ID, or {@code null} if there is no
141145
* conversation ID for the current scope
146+
* @throws IllegalStateException if the underlying scope is not currently active
142147
*/
143148
String getConversationId();
144149

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,9 @@ protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] ar
500500
* @see #instantiateUsingFactoryMethod
501501
* @see #autowireConstructor
502502
*/
503-
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
503+
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
504+
throws BeanCreationException {
505+
504506
// Instantiate the bean.
505507
BeanWrapper instanceWrapper = null;
506508
if (mbd.isSingleton()) {
@@ -515,7 +517,13 @@ protected Object doCreateBean(final String beanName, final RootBeanDefinition mb
515517
// Allow post-processors to modify the merged bean definition.
516518
synchronized (mbd.postProcessingLock) {
517519
if (!mbd.postProcessed) {
518-
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
520+
try {
521+
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
522+
}
523+
catch (Throwable ex) {
524+
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
525+
"Post-processing of merged bean definition failed", ex);
526+
}
519527
mbd.postProcessed = true;
520528
}
521529
}
@@ -550,7 +558,8 @@ public Object getObject() throws BeansException {
550558
throw (BeanCreationException) ex;
551559
}
552560
else {
553-
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
561+
throw new BeanCreationException(
562+
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
554563
}
555564
}
556565

@@ -586,7 +595,8 @@ else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
586595
registerDisposableBeanIfNecessary(beanName, bean, mbd);
587596
}
588597
catch (BeanDefinitionValidationException ex) {
589-
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
598+
throw new BeanCreationException(
599+
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
590600
}
591601

592602
return exposedObject;
@@ -624,7 +634,8 @@ protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Clas
624634
protected Class<?> determineTargetType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
625635
Class<?> targetType = mbd.getTargetType();
626636
if (targetType == null) {
627-
targetType = (mbd.getFactoryMethodName() != null ? getTypeForFactoryMethod(beanName, mbd, typesToMatch) :
637+
targetType = (mbd.getFactoryMethodName() != null ?
638+
getTypeForFactoryMethod(beanName, mbd, typesToMatch) :
628639
resolveBeanClass(mbd, beanName, typesToMatch));
629640
if (ObjectUtils.isEmpty(typesToMatch) || getTempClassLoader() == null) {
630641
mbd.setTargetType(targetType);
@@ -772,10 +783,11 @@ class Holder { Class<?> value = null; }
772783
ReflectionUtils.doWithMethods(fbClass,
773784
new ReflectionUtils.MethodCallback() {
774785
@Override
775-
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
786+
public void doWith(Method method) {
776787
if (method.getName().equals(factoryMethodName) &&
777788
FactoryBean.class.isAssignableFrom(method.getReturnType())) {
778-
objectType.value = GenericTypeResolver.resolveReturnTypeArgument(method, FactoryBean.class);
789+
objectType.value = GenericTypeResolver.resolveReturnTypeArgument(
790+
method, FactoryBean.class);
779791
}
780792
}
781793
});
@@ -926,24 +938,15 @@ private FactoryBean<?> getNonSingletonFactoryBeanForTypeCheck(String beanName, R
926938
* @param mbd the merged bean definition for the bean
927939
* @param beanType the actual type of the managed bean instance
928940
* @param beanName the name of the bean
929-
* @throws BeansException if any post-processing failed
930941
* @see MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition
931942
*/
932-
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName)
933-
throws BeansException {
934-
935-
try {
936-
for (BeanPostProcessor bp : getBeanPostProcessors()) {
937-
if (bp instanceof MergedBeanDefinitionPostProcessor) {
938-
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
939-
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
940-
}
943+
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
944+
for (BeanPostProcessor bp : getBeanPostProcessors()) {
945+
if (bp instanceof MergedBeanDefinitionPostProcessor) {
946+
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
947+
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
941948
}
942949
}
943-
catch (Exception ex) {
944-
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
945-
"Post-processing failed of bean type [" + beanType + "] failed", ex);
946-
}
947950
}
948951

949952
/**
@@ -980,12 +983,9 @@ protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition
980983
* @param beanClass the class of the bean to be instantiated
981984
* @param beanName the name of the bean
982985
* @return the bean object to use instead of a default instance of the target bean, or {@code null}
983-
* @throws BeansException if any post-processing failed
984986
* @see InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
985987
*/
986-
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName)
987-
throws BeansException {
988-
988+
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
989989
for (BeanPostProcessor bp : getBeanPostProcessors()) {
990990
if (bp instanceof InstantiationAwareBeanPostProcessor) {
991991
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
@@ -1106,7 +1106,8 @@ public Object run() {
11061106
return bw;
11071107
}
11081108
catch (Throwable ex) {
1109-
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
1109+
throw new BeanCreationException(
1110+
mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
11101111
}
11111112
}
11121113

@@ -1658,7 +1659,9 @@ public Object run() throws Exception {
16581659
* methods with arguments.
16591660
* @see #invokeInitMethods
16601661
*/
1661-
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
1662+
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd)
1663+
throws Throwable {
1664+
16621665
String initMethodName = mbd.getInitMethodName();
16631666
final Method initMethod = (mbd.isNonPublicAccessAllowed() ?
16641667
BeanUtils.findMethod(bean.getClass(), initMethodName) :

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ protected <T> T doGetBean(
287287
// Guarantee initialization of beans that the current bean depends on.
288288
String[] dependsOn = mbd.getDependsOn();
289289
if (dependsOn != null) {
290-
for (String dependsOnBean : dependsOn) {
291-
if (isDependent(beanName, dependsOnBean)) {
290+
for (String dep : dependsOn) {
291+
if (isDependent(beanName, dep)) {
292292
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
293-
"Circular depends-on relationship between '" + beanName + "' and '" + dependsOnBean + "'");
293+
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
294294
}
295-
registerDependentBean(dependsOnBean, beanName);
296-
getBean(dependsOnBean);
295+
registerDependentBean(dep, beanName);
296+
getBean(dep);
297297
}
298298
}
299299

@@ -460,19 +460,19 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
460460
return false;
461461
}
462462
if (isFactoryBean(beanName, mbd)) {
463-
final FactoryBean<?> factoryBean = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
463+
final FactoryBean<?> fb = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
464464
if (System.getSecurityManager() != null) {
465465
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
466466
@Override
467467
public Boolean run() {
468-
return ((factoryBean instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factoryBean).isPrototype()) ||
469-
!factoryBean.isSingleton());
468+
return ((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) ||
469+
!fb.isSingleton());
470470
}
471471
}, getAccessControlContext());
472472
}
473473
else {
474-
return ((factoryBean instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factoryBean).isPrototype()) ||
475-
!factoryBean.isSingleton());
474+
return ((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) ||
475+
!fb.isSingleton());
476476
}
477477
}
478478
else {
@@ -800,12 +800,15 @@ public void addEmbeddedValueResolver(StringValueResolver valueResolver) {
800800

801801
@Override
802802
public String resolveEmbeddedValue(String value) {
803+
if (value == null) {
804+
return null;
805+
}
803806
String result = value;
804807
for (StringValueResolver resolver : this.embeddedValueResolvers) {
808+
result = resolver.resolveStringValue(result);
805809
if (result == null) {
806810
return null;
807811
}
808-
result = resolver.resolveStringValue(result);
809812
}
810813
return result;
811814
}
@@ -1045,11 +1048,11 @@ public void destroyBean(String beanName, Object beanInstance) {
10451048
* Destroy the given bean instance (usually a prototype instance
10461049
* obtained from this factory) according to the given bean definition.
10471050
* @param beanName the name of the bean definition
1048-
* @param beanInstance the bean instance to destroy
1051+
* @param bean the bean instance to destroy
10491052
* @param mbd the merged bean definition
10501053
*/
1051-
protected void destroyBean(String beanName, Object beanInstance, RootBeanDefinition mbd) {
1052-
new DisposableBeanAdapter(beanInstance, beanName, mbd, getBeanPostProcessors(), getAccessControlContext()).destroy();
1054+
protected void destroyBean(String beanName, Object bean, RootBeanDefinition mbd) {
1055+
new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), getAccessControlContext()).destroy();
10531056
}
10541057

10551058
@Override
@@ -1230,12 +1233,13 @@ protected RootBeanDefinition getMergedBeanDefinition(
12301233
pbd = getMergedBeanDefinition(parentBeanName);
12311234
}
12321235
else {
1233-
if (getParentBeanFactory() instanceof ConfigurableBeanFactory) {
1234-
pbd = ((ConfigurableBeanFactory) getParentBeanFactory()).getMergedBeanDefinition(parentBeanName);
1236+
BeanFactory parent = getParentBeanFactory();
1237+
if (parent instanceof ConfigurableBeanFactory) {
1238+
pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName);
12351239
}
12361240
else {
1237-
throw new NoSuchBeanDefinitionException(bd.getParentName(),
1238-
"Parent name '" + bd.getParentName() + "' is equal to bean name '" + beanName +
1241+
throw new NoSuchBeanDefinitionException(parentBeanName,
1242+
"Parent name '" + parentBeanName + "' is equal to bean name '" + beanName +
12391243
"': cannot be resolved without an AbstractBeanFactory parent");
12401244
}
12411245
}
@@ -1351,12 +1355,14 @@ public Class<?> run() throws Exception {
13511355
catch (ClassNotFoundException ex) {
13521356
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
13531357
}
1354-
catch (LinkageError err) {
1355-
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), err);
1358+
catch (LinkageError ex) {
1359+
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
13561360
}
13571361
}
13581362

1359-
private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToMatch) throws ClassNotFoundException {
1363+
private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToMatch)
1364+
throws ClassNotFoundException {
1365+
13601366
ClassLoader beanClassLoader = getBeanClassLoader();
13611367
ClassLoader classLoaderToUse = beanClassLoader;
13621368
if (!ObjectUtils.isEmpty(typesToMatch)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
214214
if (singletonObject == null) {
215215
if (this.singletonsCurrentlyInDestruction) {
216216
throw new BeanCreationNotAllowedException(beanName,
217-
"Singleton bean creation not allowed while the singletons of this factory are in destruction " +
217+
"Singleton bean creation not allowed while singletons of this factory are in destruction " +
218218
"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
219219
}
220220
if (logger.isDebugEnabled()) {

0 commit comments

Comments
 (0)