Skip to content

Commit 26ee0c4

Browse files
committed
aligned with recent changes in CommonAnnotationBeanPostProcessor; backported NPE check
Issue: SPR-9176 Issue: SPR-9627 Issue: SPR-9316
1 parent 1971870 commit 26ee0c4

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -405,10 +405,16 @@ protected <T> Map<String, T> findAutowireCandidates(Class<T> type) throws BeansE
405405
protected boolean determineRequiredStatus(Annotation annotation) {
406406
try {
407407
Method method = ReflectionUtils.findMethod(annotation.annotationType(), this.requiredParameterName);
408+
if (method == null) {
409+
// annotations like @Inject and @Value don't have a method (attribute) named "required"
410+
// -> default to required status
411+
return true;
412+
}
408413
return (this.requiredParameterValue == (Boolean) ReflectionUtils.invokeMethod(method, annotation));
409414
}
410415
catch (Exception ex) {
411-
// required by default
416+
// an exception was thrown during reflective invocation of the required attribute
417+
// -> default to required status
412418
return true;
413419
}
414420
}
@@ -419,11 +425,12 @@ protected boolean determineRequiredStatus(Annotation annotation) {
419425
private void registerDependentBeans(String beanName, Set<String> autowiredBeanNames) {
420426
if (beanName != null) {
421427
for (String autowiredBeanName : autowiredBeanNames) {
422-
beanFactory.registerDependentBean(autowiredBeanName, beanName);
428+
if (this.beanFactory.containsBean(autowiredBeanName)) {
429+
this.beanFactory.registerDependentBean(autowiredBeanName, beanName);
430+
}
423431
if (logger.isDebugEnabled()) {
424-
logger.debug(
425-
"Autowiring by type from bean name '" + beanName + "' to bean named '" + autowiredBeanName +
426-
"'");
432+
logger.debug("Autowiring by type from bean name '" + beanName +
433+
"' to bean named '" + autowiredBeanName + "'");
427434
}
428435
}
429436
}
@@ -435,11 +442,11 @@ private void registerDependentBeans(String beanName, Set<String> autowiredBeanNa
435442
private Object resolvedCachedArgument(String beanName, Object cachedArgument) {
436443
if (cachedArgument instanceof DependencyDescriptor) {
437444
DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument;
438-
TypeConverter typeConverter = beanFactory.getTypeConverter();
439-
return beanFactory.resolveDependency(descriptor, beanName, null, typeConverter);
445+
TypeConverter typeConverter = this.beanFactory.getTypeConverter();
446+
return this.beanFactory.resolveDependency(descriptor, beanName, null, typeConverter);
440447
}
441448
else if (cachedArgument instanceof RuntimeBeanReference) {
442-
return beanFactory.getBean(((RuntimeBeanReference) cachedArgument).getBeanName());
449+
return this.beanFactory.getBean(((RuntimeBeanReference) cachedArgument).getBeanName());
443450
}
444451
else {
445452
return cachedArgument;

org.springframework.beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -118,6 +118,11 @@ public void testExtendedResourceInjection() {
118118
assertSame(tb, bean.getTestBean4());
119119
assertSame(ntb, bean.getNestedTestBean());
120120
assertSame(bf, bean.getBeanFactory());
121+
122+
String[] depBeans = bf.getDependenciesForBean("annotatedBean");
123+
assertEquals(2, depBeans.length);
124+
assertEquals("testBean", depBeans[0]);
125+
assertEquals("nestedTestBean", depBeans[1]);
121126
}
122127

123128
@Test

0 commit comments

Comments
 (0)