Detect @Autowired constructors for configuration classes
Issue: SPR-13471
This commit is contained in:
parent
9e7c791a0f
commit
967ef73765
|
@ -271,6 +271,19 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
Constructor<?> defaultConstructor = null;
|
Constructor<?> defaultConstructor = null;
|
||||||
for (Constructor<?> candidate : rawCandidates) {
|
for (Constructor<?> candidate : rawCandidates) {
|
||||||
AnnotationAttributes ann = findAutowiredAnnotation(candidate);
|
AnnotationAttributes ann = findAutowiredAnnotation(candidate);
|
||||||
|
if (ann == null) {
|
||||||
|
Class<?> userClass = ClassUtils.getUserClass(beanClass);
|
||||||
|
if (userClass != beanClass) {
|
||||||
|
try {
|
||||||
|
Constructor<?> superCtor =
|
||||||
|
userClass.getDeclaredConstructor(candidate.getParameterTypes());
|
||||||
|
ann = findAutowiredAnnotation(superCtor);
|
||||||
|
}
|
||||||
|
catch (NoSuchMethodException ex) {
|
||||||
|
// Simply proceed, no equivalent superclass constructor found...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (ann != null) {
|
if (ann != null) {
|
||||||
if (requiredConstructor != null) {
|
if (requiredConstructor != null) {
|
||||||
throw new BeanCreationException(beanName,
|
throw new BeanCreationException(beanName,
|
||||||
|
|
|
@ -23,7 +23,6 @@ import javax.inject.Provider;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
@ -90,7 +89,7 @@ public class AutowiredConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAutowiredConfigurationConstructorsAreSupported() {
|
public void testAutowiredSingleConstructorSupported() {
|
||||||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||||
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
|
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
|
||||||
new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
|
new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
|
||||||
|
@ -98,8 +97,19 @@ public class AutowiredConfigurationTests {
|
||||||
ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
|
ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
|
||||||
ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
|
ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
assertSame(ctx.getBean(AutowiredConstructorConfig.class).colour,
|
assertSame(ctx.getBean(AutowiredConstructorConfig.class).colour, ctx.getBean(Colour.class));
|
||||||
ctx.getBean(Colour.class));
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAutowiredAnnotatedConstructorSupported() {
|
||||||
|
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||||
|
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
|
||||||
|
new ClassPathResource("annotation-config.xml", MultipleConstructorConfig.class));
|
||||||
|
GenericApplicationContext ctx = new GenericApplicationContext(factory);
|
||||||
|
ctx.registerBeanDefinition("config1", new RootBeanDefinition(MultipleConstructorConfig.class));
|
||||||
|
ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
|
||||||
|
ctx.refresh();
|
||||||
|
assertSame(ctx.getBean(MultipleConstructorConfig.class).colour, ctx.getBean(Colour.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -223,13 +233,29 @@ public class AutowiredConfigurationTests {
|
||||||
|
|
||||||
Colour colour;
|
Colour colour;
|
||||||
|
|
||||||
@Autowired
|
// @Autowired
|
||||||
AutowiredConstructorConfig(Colour colour) {
|
AutowiredConstructorConfig(Colour colour) {
|
||||||
this.colour = colour;
|
this.colour = colour;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class MultipleConstructorConfig {
|
||||||
|
|
||||||
|
Colour colour;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MultipleConstructorConfig(Colour colour) {
|
||||||
|
this.colour = colour;
|
||||||
|
}
|
||||||
|
|
||||||
|
MultipleConstructorConfig(String test) {
|
||||||
|
this.colour = new Colour(test);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class ColorConfig {
|
static class ColorConfig {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue