Skip to content

Commit b3cd1ad

Browse files
committed
Refined throwing of BeanCreationExceptions (and reflection exceptions)
Issue: SPR-14883
1 parent cf479bf commit b3cd1ad

File tree

6 files changed

+18
-43
lines changed

6 files changed

+18
-43
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0)
355355

356356
@Override
357357
public PropertyValues postProcessPropertyValues(
358-
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
358+
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException {
359359

360360
InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
361361
try {
@@ -404,14 +404,8 @@ private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz
404404
if (metadata != null) {
405405
metadata.clear(pvs);
406406
}
407-
try {
408-
metadata = buildAutowiringMetadata(clazz);
409-
this.injectionMetadataCache.put(cacheKey, metadata);
410-
}
411-
catch (NoClassDefFoundError err) {
412-
throw new IllegalStateException("Failed to introspect bean class [" + clazz.getName() +
413-
"] for autowiring metadata: could not find class that it depends on", err);
414-
}
407+
metadata = buildAutowiringMetadata(clazz);
408+
this.injectionMetadataCache.put(cacheKey, metadata);
415409
}
416410
}
417411
}

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
145145
private final Set<Class<?>> ignoredDependencyInterfaces = new HashSet<>();
146146

147147
/** Cache of unfinished FactoryBean instances: FactoryBean name --> BeanWrapper */
148-
private final Map<String, BeanWrapper> factoryBeanInstanceCache =
149-
new ConcurrentHashMap<>(16);
148+
private final Map<String, BeanWrapper> factoryBeanInstanceCache = new ConcurrentHashMap<>(16);
150149

151150
/** Cache of filtered PropertyDescriptors: bean Class -> PropertyDescriptor array */
152151
private final ConcurrentMap<Class<?>, PropertyDescriptor[]> filteredPropertyDescriptorsCache =
@@ -536,7 +535,7 @@ protected Object doCreateBean(final String beanName, final RootBeanDefinition mb
536535
}
537536
catch (Throwable ex) {
538537
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
539-
"Post-processing of bean type [" + beanType.getName() + "] failed", ex);
538+
"Post-processing of merged bean definition failed", ex);
540539
}
541540
mbd.postProcessed = true;
542541
}
@@ -721,8 +720,7 @@ protected Class<?> getTypeForFactoryMethod(String beanName, RootBeanDefinition m
721720
paramNames = pnd.getParameterNames(factoryMethod);
722721
}
723722
ConstructorArgumentValues cav = mbd.getConstructorArgumentValues();
724-
Set<ConstructorArgumentValues.ValueHolder> usedValueHolders =
725-
new HashSet<>(paramTypes.length);
723+
Set<ConstructorArgumentValues.ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length);
726724
Object[] args = new Object[paramTypes.length];
727725
for (int i = 0; i < args.length; i++) {
728726
ConstructorArgumentValues.ValueHolder valueHolder = cav.getArgumentValue(
@@ -952,12 +950,9 @@ private FactoryBean<?> getNonSingletonFactoryBeanForTypeCheck(String beanName, R
952950
* @param mbd the merged bean definition for the bean
953951
* @param beanType the actual type of the managed bean instance
954952
* @param beanName the name of the bean
955-
* @throws BeansException if any post-processing failed
956953
* @see MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition
957954
*/
958-
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName)
959-
throws BeansException {
960-
955+
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
961956
for (BeanPostProcessor bp : getBeanPostProcessors()) {
962957
if (bp instanceof MergedBeanDefinitionPostProcessor) {
963958
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
@@ -1000,12 +995,9 @@ protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition
1000995
* @param beanClass the class of the bean to be instantiated
1001996
* @param beanName the name of the bean
1002997
* @return the bean object to use instead of a default instance of the target bean, or {@code null}
1003-
* @throws BeansException if any post-processing failed
1004998
* @see InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
1005999
*/
1006-
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName)
1007-
throws BeansException {
1008-
1000+
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
10091001
for (BeanPostProcessor bp : getBeanPostProcessors()) {
10101002
if (bp instanceof InstantiationAwareBeanPostProcessor) {
10111003
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package org.springframework.beans.factory.support;
1818

1919
/**
20-
* Internal exception to be propagated from {@link ConstructorResolver}.
20+
* Internal exception to be propagated from {@link ConstructorResolver},
21+
* passed through to the initiating {@link DefaultSingletonBeanRegistry}
22+
* (without wrapping in a {@code BeanCreationException}).
2123
*
24+
* @author Juergen Hoeller
2225
* @since 5.0
2326
*/
2427
@SuppressWarnings("serial")

spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,8 @@ private InjectionMetadata findResourceMetadata(String beanName, final Class<?> c
336336
if (metadata != null) {
337337
metadata.clear(pvs);
338338
}
339-
try {
340-
metadata = buildResourceMetadata(clazz);
341-
this.injectionMetadataCache.put(cacheKey, metadata);
342-
}
343-
catch (NoClassDefFoundError err) {
344-
throw new IllegalStateException("Failed to introspect bean class [" + clazz.getName() +
345-
"] for resource metadata: could not find class that it depends on", err);
346-
}
339+
metadata = buildResourceMetadata(clazz);
340+
this.injectionMetadataCache.put(cacheKey, metadata);
347341
}
348342
}
349343
}

spring-core/src/main/java/org/springframework/util/ReflectionUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@ public abstract class ReflectionUtils {
6060
* Cache for {@link Class#getDeclaredMethods()} plus equivalent default methods
6161
* from Java 8 based interfaces, allowing for fast iteration.
6262
*/
63-
private static final Map<Class<?>, Method[]> declaredMethodsCache =
64-
new ConcurrentReferenceHashMap<>(256);
63+
private static final Map<Class<?>, Method[]> declaredMethodsCache = new ConcurrentReferenceHashMap<>(256);
6564

6665
/**
6766
* Cache for {@link Class#getDeclaredFields()}, allowing for fast iteration.
6867
*/
69-
private static final Map<Class<?>, Field[]> declaredFieldsCache =
70-
new ConcurrentReferenceHashMap<>(256);
68+
private static final Map<Class<?>, Field[]> declaredFieldsCache = new ConcurrentReferenceHashMap<>(256);
7169

7270

7371
/**

spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,8 @@ private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?
389389
if (metadata != null) {
390390
metadata.clear(pvs);
391391
}
392-
try {
393-
metadata = buildPersistenceMetadata(clazz);
394-
this.injectionMetadataCache.put(cacheKey, metadata);
395-
}
396-
catch (NoClassDefFoundError err) {
397-
throw new IllegalStateException("Failed to introspect bean class [" + clazz.getName() +
398-
"] for persistence metadata: could not find class that it depends on", err);
399-
}
392+
metadata = buildPersistenceMetadata(clazz);
393+
this.injectionMetadataCache.put(cacheKey, metadata);
400394
}
401395
}
402396
}

0 commit comments

Comments
 (0)