Skip to content

Commit 80cec01

Browse files
committed
Log warning for single optional constructor when no default constructor to fall back to
Issue: SPR-12161
1 parent 15320db commit 80cec01

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,16 @@ else if (candidate.getParameterTypes().length == 0) {
299299
}
300300
if (!candidates.isEmpty()) {
301301
// Add default constructor to list of optional constructors, as fallback.
302-
if (requiredConstructor == null && defaultConstructor != null) {
303-
candidates.add(defaultConstructor);
302+
if (requiredConstructor == null) {
303+
if (defaultConstructor != null) {
304+
candidates.add(defaultConstructor);
305+
}
306+
else if (candidates.size() == 1 && logger.isWarnEnabled()) {
307+
logger.warn("Inconsistent constructor declaration on bean with name '" + beanName +
308+
"': single autowire-marked constructor flagged as optional - this constructor " +
309+
"is effectively required since there is no default constructor to fall back to: " +
310+
candidates.get(0));
311+
}
304312
}
305313
candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
306314
}

Diff for: spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

+33-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.beans.factory.BeanFactory;
3636
import org.springframework.beans.factory.FactoryBean;
3737
import org.springframework.beans.factory.ObjectFactory;
38+
import org.springframework.beans.factory.UnsatisfiedDependencyException;
3839
import org.springframework.beans.factory.config.BeanDefinition;
3940
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4041
import org.springframework.beans.factory.config.TypedStringValue;
@@ -52,7 +53,6 @@
5253
import org.springframework.util.SerializationTestUtils;
5354

5455
import static org.junit.Assert.*;
55-
import static org.mockito.Mockito.*;
5656

5757
/**
5858
* @author Juergen Hoeller
@@ -552,6 +552,23 @@ public void testConstructorResourceInjectionWithMultipleCandidates() {
552552
bf.destroySingletons();
553553
}
554554

555+
@Test
556+
public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() {
557+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
558+
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
559+
bpp.setBeanFactory(bf);
560+
bf.addBeanPostProcessor(bpp);
561+
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class));
562+
563+
try {
564+
bf.getBean("annotatedBean");
565+
fail("Should have thrown UnsatisfiedDependencyException");
566+
}
567+
catch (UnsatisfiedDependencyException ex) {
568+
// expected
569+
}
570+
}
571+
555572
@Test
556573
public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() {
557574
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -2048,6 +2065,21 @@ public NestedTestBean[] getNestedTestBeans() {
20482065
}
20492066

20502067

2068+
public static class ConstructorWithoutFallbackBean {
2069+
2070+
protected ITestBean testBean3;
2071+
2072+
@Autowired(required = false)
2073+
public ConstructorWithoutFallbackBean(ITestBean testBean3) {
2074+
this.testBean3 = testBean3;
2075+
}
2076+
2077+
public ITestBean getTestBean3() {
2078+
return this.testBean3;
2079+
}
2080+
}
2081+
2082+
20512083
public static class ConstructorsCollectionResourceInjectionBean {
20522084

20532085
protected ITestBean testBean3;
@@ -2113,7 +2145,6 @@ public static class MapFieldInjectionBean {
21132145
@Autowired
21142146
private Map<String, TestBean> testBeanMap;
21152147

2116-
21172148
public Map<String, TestBean> getTestBeanMap() {
21182149
return this.testBeanMap;
21192150
}

0 commit comments

Comments
 (0)