@@ -220,7 +220,7 @@ public int getOrder() {
220
220
}
221
221
222
222
@ Override
223
- public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
223
+ public void setBeanFactory (BeanFactory beanFactory ) {
224
224
if (!(beanFactory instanceof ConfigurableListableBeanFactory )) {
225
225
throw new IllegalArgumentException (
226
226
"AutowiredAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory" );
@@ -238,7 +238,10 @@ public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, C
238
238
}
239
239
240
240
@ Override
241
- public Constructor <?>[] determineCandidateConstructors (Class <?> beanClass , final String beanName ) throws BeansException {
241
+ public Constructor <?>[] determineCandidateConstructors (Class <?> beanClass , final String beanName )
242
+ throws BeanCreationException {
243
+
244
+ // Let's check for lookup methods here..
242
245
if (!this .lookupMethodsChecked .contains (beanName )) {
243
246
ReflectionUtils .doWithMethods (beanClass , new ReflectionUtils .MethodCallback () {
244
247
@ Override
@@ -263,10 +266,19 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
263
266
// Quick check on the concurrent map first, with minimal locking.
264
267
Constructor <?>[] candidateConstructors = this .candidateConstructorsCache .get (beanClass );
265
268
if (candidateConstructors == null ) {
269
+ // Fully synchronized resolution now...
266
270
synchronized (this .candidateConstructorsCache ) {
267
271
candidateConstructors = this .candidateConstructorsCache .get (beanClass );
268
272
if (candidateConstructors == null ) {
269
- Constructor <?>[] rawCandidates = beanClass .getDeclaredConstructors ();
273
+ Constructor <?>[] rawCandidates ;
274
+ try {
275
+ rawCandidates = beanClass .getDeclaredConstructors ();
276
+ }
277
+ catch (Throwable ex ) {
278
+ throw new BeanCreationException (beanName ,
279
+ "Resolution of declared constructors on bean Class [" + beanClass .getName () +
280
+ "] from ClassLoader [" + beanClass .getClassLoader () + "] failed" , ex );
281
+ }
270
282
List <Constructor <?>> candidates = new ArrayList <Constructor <?>>(rawCandidates .length );
271
283
Constructor <?> requiredConstructor = null ;
272
284
Constructor <?> defaultConstructor = null ;
@@ -320,9 +332,9 @@ else if (candidate.getParameterTypes().length == 0) {
320
332
}
321
333
else if (candidates .size () == 1 && logger .isWarnEnabled ()) {
322
334
logger .warn ("Inconsistent constructor declaration on bean with name '" + beanName +
323
- "': single autowire-marked constructor flagged as optional - this constructor " +
324
- "is effectively required since there is no default constructor to fall back to: " +
325
- candidates .get (0 ));
335
+ "': single autowire-marked constructor flagged as optional - " +
336
+ "this constructor is effectively required since there is no " +
337
+ "default constructor to fall back to: " + candidates .get (0 ));
326
338
}
327
339
}
328
340
candidateConstructors = candidates .toArray (new Constructor <?>[candidates .size ()]);
@@ -342,7 +354,7 @@ else if (rawCandidates.length == 1 && rawCandidates[0].getParameterTypes().lengt
342
354
343
355
@ Override
344
356
public PropertyValues postProcessPropertyValues (
345
- PropertyValues pvs , PropertyDescriptor [] pds , Object bean , String beanName ) throws BeansException {
357
+ PropertyValues pvs , PropertyDescriptor [] pds , Object bean , String beanName ) throws BeanCreationException {
346
358
347
359
InjectionMetadata metadata = findAutowiringMetadata (beanName , bean .getClass (), pvs );
348
360
try {
@@ -361,9 +373,9 @@ public PropertyValues postProcessPropertyValues(
361
373
* 'Native' processing method for direct calls with an arbitrary target instance,
362
374
* resolving all of its fields and methods which are annotated with {@code @Autowired}.
363
375
* @param bean the target instance to process
364
- * @throws BeansException if autowiring failed
376
+ * @throws BeanCreationException if autowiring failed
365
377
*/
366
- public void processInjection (Object bean ) throws BeansException {
378
+ public void processInjection (Object bean ) throws BeanCreationException {
367
379
Class <?> clazz = bean .getClass ();
368
380
InjectionMetadata metadata = findAutowiringMetadata (clazz .getName (), clazz , null );
369
381
try {
@@ -373,7 +385,8 @@ public void processInjection(Object bean) throws BeansException {
373
385
throw ex ;
374
386
}
375
387
catch (Throwable ex ) {
376
- throw new BeanCreationException ("Injection of autowired dependencies failed for class [" + clazz + "]" , ex );
388
+ throw new BeanCreationException (
389
+ "Injection of autowired dependencies failed for class [" + clazz + "]" , ex );
377
390
}
378
391
}
379
392
@@ -446,7 +459,8 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
446
459
}
447
460
if (method .getParameterTypes ().length == 0 ) {
448
461
if (logger .isWarnEnabled ()) {
449
- logger .warn ("Autowired annotation should be used on methods with parameters: " + method );
462
+ logger .warn ("Autowired annotation should only be used on methods with parameters: " +
463
+ method );
450
464
}
451
465
}
452
466
boolean required = determineRequiredStatus (ann );
@@ -629,15 +643,15 @@ protected void inject(Object bean, String beanName, PropertyValues pvs) throws T
629
643
Class <?>[] paramTypes = method .getParameterTypes ();
630
644
arguments = new Object [paramTypes .length ];
631
645
DependencyDescriptor [] descriptors = new DependencyDescriptor [paramTypes .length ];
632
- Set <String > autowiredBeanNames = new LinkedHashSet <String >(paramTypes .length );
646
+ Set <String > autowiredBeans = new LinkedHashSet <String >(paramTypes .length );
633
647
TypeConverter typeConverter = beanFactory .getTypeConverter ();
634
648
for (int i = 0 ; i < arguments .length ; i ++) {
635
649
MethodParameter methodParam = new MethodParameter (method , i );
636
650
DependencyDescriptor currDesc = new DependencyDescriptor (methodParam , this .required );
637
651
currDesc .setContainingClass (bean .getClass ());
638
652
descriptors [i ] = currDesc ;
639
653
try {
640
- Object arg = beanFactory .resolveDependency (currDesc , beanName , autowiredBeanNames , typeConverter );
654
+ Object arg = beanFactory .resolveDependency (currDesc , beanName , autowiredBeans , typeConverter );
641
655
if (arg == null && !this .required ) {
642
656
arguments = null ;
643
657
break ;
@@ -655,9 +669,9 @@ protected void inject(Object bean, String beanName, PropertyValues pvs) throws T
655
669
for (int i = 0 ; i < arguments .length ; i ++) {
656
670
this .cachedMethodArguments [i ] = descriptors [i ];
657
671
}
658
- registerDependentBeans (beanName , autowiredBeanNames );
659
- if (autowiredBeanNames .size () == paramTypes .length ) {
660
- Iterator <String > it = autowiredBeanNames .iterator ();
672
+ registerDependentBeans (beanName , autowiredBeans );
673
+ if (autowiredBeans .size () == paramTypes .length ) {
674
+ Iterator <String > it = autowiredBeans .iterator ();
661
675
for (int i = 0 ; i < paramTypes .length ; i ++) {
662
676
String autowiredBeanName = it .next ();
663
677
if (beanFactory .containsBean (autowiredBeanName )) {
@@ -706,19 +720,19 @@ private Object[] resolveCachedArguments(String beanName) {
706
720
@ SuppressWarnings ("serial" )
707
721
private static class ShortcutDependencyDescriptor extends DependencyDescriptor {
708
722
709
- private final String shortcutName ;
723
+ private final String shortcut ;
710
724
711
725
private final Class <?> requiredType ;
712
726
713
- public ShortcutDependencyDescriptor (DependencyDescriptor original , String shortcutName , Class <?> requiredType ) {
727
+ public ShortcutDependencyDescriptor (DependencyDescriptor original , String shortcut , Class <?> requiredType ) {
714
728
super (original );
715
- this .shortcutName = shortcutName ;
729
+ this .shortcut = shortcut ;
716
730
this .requiredType = requiredType ;
717
731
}
718
732
719
733
@ Override
720
734
public Object resolveShortcut (BeanFactory beanFactory ) {
721
- return resolveCandidate (this .shortcutName , this .requiredType , beanFactory );
735
+ return resolveCandidate (this .shortcut , this .requiredType , beanFactory );
722
736
}
723
737
}
724
738
0 commit comments