Skip to content

Commit 092de01

Browse files
committed
improved NoClassDefFoundError handling during constructor resolution (SPR-5522)
1 parent b80362f commit 092de01

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 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.
@@ -102,7 +102,7 @@ public ConstructorResolver(AbstractBeanFactory beanFactory, AutowireCapableBeanF
102102
* or <code>null</code> if none (-> use constructor argument values from bean definition)
103103
* @return a BeanWrapper for the new instance
104104
*/
105-
protected BeanWrapper autowireConstructor(
105+
public BeanWrapper autowireConstructor(
106106
String beanName, RootBeanDefinition mbd, Constructor[] chosenCtors, Object[] explicitArgs) {
107107

108108
BeanWrapperImpl bw = new BeanWrapperImpl();
@@ -148,7 +148,7 @@ else if (argValue instanceof BeanMetadataElement) {
148148
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
149149
ConstructorArgumentValues resolvedValues = null;
150150

151-
int minNrOfArgs = 0;
151+
int minNrOfArgs;
152152
if (explicitArgs != null) {
153153
minNrOfArgs = explicitArgs.length;
154154
}
@@ -159,8 +159,18 @@ else if (argValue instanceof BeanMetadataElement) {
159159
}
160160

161161
// Take specified constructors, if any.
162-
Constructor[] candidates =
163-
(chosenCtors != null ? chosenCtors : mbd.getBeanClass().getDeclaredConstructors());
162+
Constructor[] candidates = chosenCtors;
163+
if (candidates == null) {
164+
Class beanClass = mbd.getBeanClass();
165+
try {
166+
candidates = beanClass.getDeclaredConstructors();
167+
}
168+
catch (Throwable ex) {
169+
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
170+
"Resolution of declared constructors on bean Class [" + beanClass.getName() +
171+
"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
172+
}
173+
}
164174
AutowireUtils.sortConstructors(candidates);
165175
int minTypeDiffWeight = Integer.MAX_VALUE;
166176

@@ -180,7 +190,7 @@ else if (argValue instanceof BeanMetadataElement) {
180190
"(hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)");
181191
}
182192

183-
ArgumentsHolder args = null;
193+
ArgumentsHolder args;
184194
List<Exception> causes = null;
185195

186196
if (resolvedValues != null) {
@@ -270,9 +280,9 @@ public BeanWrapper instantiateUsingFactoryMethod(String beanName, RootBeanDefini
270280
BeanWrapperImpl bw = new BeanWrapperImpl();
271281
this.beanFactory.initBeanWrapper(bw);
272282

273-
Class factoryClass = null;
274-
Object factoryBean = null;
275-
boolean isStatic = true;
283+
Object factoryBean;
284+
Class factoryClass;
285+
boolean isStatic;
276286

277287
String factoryBeanName = mbd.getFactoryBeanName();
278288
if (factoryBeanName != null) {
@@ -290,7 +300,9 @@ public BeanWrapper instantiateUsingFactoryMethod(String beanName, RootBeanDefini
290300
}
291301
else {
292302
// It's a static factory method on the bean class.
303+
factoryBean = null;
293304
factoryClass = mbd.getBeanClass();
305+
isStatic = true;
294306
}
295307

296308
Method factoryMethodToUse = null;
@@ -335,7 +347,7 @@ else if (argValue instanceof BeanMetadataElement) {
335347
int minTypeDiffWeight = Integer.MAX_VALUE;
336348
ConstructorArgumentValues resolvedValues = null;
337349

338-
int minNrOfArgs = 0;
350+
int minNrOfArgs;
339351
if (explicitArgs != null) {
340352
minNrOfArgs = explicitArgs.length;
341353
}
@@ -357,7 +369,7 @@ else if (argValue instanceof BeanMetadataElement) {
357369
candidate.getName().equals(mbd.getFactoryMethodName()) &&
358370
paramTypes.length >= minNrOfArgs) {
359371

360-
ArgumentsHolder args = null;
372+
ArgumentsHolder args;
361373

362374
if (resolvedValues != null) {
363375
// Resolved contructor arguments: type conversion and/or autowiring necessary.

0 commit comments

Comments
 (0)