Skip to content

Commit 934fffe

Browse files
committed
AutowiredAnnotationBeanPostProcessor tolerates annotated no-arg constructors
Issue: SPR-15005 (cherry picked from commit 8b5ee4e)
1 parent 8190e78 commit 934fffe

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,6 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
313313
". Found constructor with 'required' Autowired annotation already: " +
314314
requiredConstructor);
315315
}
316-
if (candidate.getParameterTypes().length == 0) {
317-
throw new IllegalStateException(
318-
"Autowired annotation requires at least one argument: " + candidate);
319-
}
320316
boolean required = determineRequiredStatus(ann);
321317
if (required) {
322318
if (!candidates.isEmpty()) {

spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -742,6 +742,15 @@ public void testProviderOfOptionalMethodInjectionWithBeanNotAvailable() {
742742
bf.destroySingletons();
743743
}
744744

745+
@Test
746+
public void testAnnotatedDefaultConstructor() {
747+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
748+
bf.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor());
749+
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class));
750+
751+
assertNotNull(bf.getBean("annotatedBean"));
752+
}
753+
745754

746755
public static class ResourceInjectionBean {
747756

@@ -750,7 +759,6 @@ public static class ResourceInjectionBean {
750759

751760
private TestBean testBean2;
752761

753-
754762
@Inject
755763
public void setTestBean2(TestBean testBean2) {
756764
if (this.testBean2 != null) {
@@ -819,7 +827,6 @@ public BeanFactory getBeanFactory() {
819827

820828

821829
public static class TypedExtendedResourceInjectionBean extends ExtendedResourceInjectionBean<NestedTestBean> {
822-
823830
}
824831

825832

@@ -1087,7 +1094,6 @@ public static class MapFieldInjectionBean {
10871094
@Inject
10881095
private Map<String, TestBean> testBeanMap;
10891096

1090-
10911097
public Map<String, TestBean> getTestBeanMap() {
10921098
return this.testBeanMap;
10931099
}
@@ -1346,4 +1352,12 @@ public Optional<TestBean> getTestBean() {
13461352
}
13471353
}
13481354

1355+
1356+
public static class AnnotatedDefaultConstructorBean {
1357+
1358+
@Inject
1359+
public AnnotatedDefaultConstructorBean() {
1360+
}
1361+
}
1362+
13491363
}

0 commit comments

Comments
 (0)