Skip to content

Commit 8b5ee4e

Browse files
committed
AutowiredAnnotationBeanPostProcessor tolerates annotated no-arg constructors
Issue: SPR-15005
1 parent b825528 commit 8b5ee4e

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-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
@@ -305,10 +305,6 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
305305
". Found constructor with 'required' Autowired annotation already: " +
306306
requiredConstructor);
307307
}
308-
if (candidate.getParameterCount() == 0) {
309-
throw new IllegalStateException(
310-
"Autowired annotation requires at least one argument: " + candidate);
311-
}
312308
boolean required = determineRequiredStatus(ann);
313309
if (required) {
314310
if (!candidates.isEmpty()) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,15 @@ public void testSingleConstructorWithProvidedArgument() {
21842184
assertNotNull(bf.getBean(ProvidedArgumentBean.class));
21852185
}
21862186

2187+
@Test
2188+
public void testAnnotatedDefaultConstructor() {
2189+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
2190+
bf.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor());
2191+
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class));
2192+
2193+
assertNotNull(bf.getBean("annotatedBean"));
2194+
}
2195+
21872196

21882197
public static class ResourceInjectionBean {
21892198

@@ -3411,7 +3420,14 @@ public static Set<TestBean> testBeanSet() {
34113420
tbs.add(new TestBean("tb2"));
34123421
return tbs;
34133422
}
3423+
}
3424+
3425+
3426+
public static class AnnotatedDefaultConstructorBean {
34143427

3428+
@Autowired
3429+
public AnnotatedDefaultConstructorBean() {
3430+
}
34153431
}
34163432

34173433
}

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)