From 8b5ee4ef9150dd3294e005225c215a1fa6f52fa1 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 12 Dec 2016 22:26:24 +0100 Subject: [PATCH] AutowiredAnnotationBeanPostProcessor tolerates annotated no-arg constructors Issue: SPR-15005 --- .../AutowiredAnnotationBeanPostProcessor.java | 4 ---- ...wiredAnnotationBeanPostProcessorTests.java | 16 ++++++++++++++ ...njectAnnotationBeanPostProcessorTests.java | 22 +++++++++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index f45308037f9..daf57d11aaf 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -305,10 +305,6 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean ". Found constructor with 'required' Autowired annotation already: " + requiredConstructor); } - if (candidate.getParameterCount() == 0) { - throw new IllegalStateException( - "Autowired annotation requires at least one argument: " + candidate); - } boolean required = determineRequiredStatus(ann); if (required) { if (!candidates.isEmpty()) { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java index ec83ce05899..f7267900d8c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java @@ -2184,6 +2184,15 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertNotNull(bf.getBean(ProvidedArgumentBean.class)); } + @Test + public void testAnnotatedDefaultConstructor() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + bf.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor()); + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class)); + + assertNotNull(bf.getBean("annotatedBean")); + } + public static class ResourceInjectionBean { @@ -3411,7 +3420,14 @@ public class AutowiredAnnotationBeanPostProcessorTests { tbs.add(new TestBean("tb2")); return tbs; } + } + + + public static class AnnotatedDefaultConstructorBean { + @Autowired + public AnnotatedDefaultConstructorBean() { + } } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java index f1bea66f2bb..63390cb8ca7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -742,6 +742,15 @@ public class InjectAnnotationBeanPostProcessorTests { bf.destroySingletons(); } + @Test + public void testAnnotatedDefaultConstructor() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + bf.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor()); + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class)); + + assertNotNull(bf.getBean("annotatedBean")); + } + public static class ResourceInjectionBean { @@ -750,7 +759,6 @@ public class InjectAnnotationBeanPostProcessorTests { private TestBean testBean2; - @Inject public void setTestBean2(TestBean testBean2) { if (this.testBean2 != null) { @@ -819,7 +827,6 @@ public class InjectAnnotationBeanPostProcessorTests { public static class TypedExtendedResourceInjectionBean extends ExtendedResourceInjectionBean { - } @@ -1087,7 +1094,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Inject private Map testBeanMap; - public Map getTestBeanMap() { return this.testBeanMap; } @@ -1346,4 +1352,12 @@ public class InjectAnnotationBeanPostProcessorTests { } } + + public static class AnnotatedDefaultConstructorBean { + + @Inject + public AnnotatedDefaultConstructorBean() { + } + } + }