diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/Configuration.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/Configuration.java
index 84d406592fe..33e79acbae2 100644
--- a/org.springframework.context/src/main/java/org/springframework/context/annotation/Configuration.java
+++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/Configuration.java
@@ -45,8 +45,8 @@ import org.springframework.stereotype.Component;
*
* - Configuration classes must be non-final
*
- Configuration classes must be non-local (may not be declared within a method)
- *
- Configuration classes must have a default/no-arg constructor or at least one
- * {@link Autowired} constructor
+ *
- Configuration classes must have a default/no-arg constructor and may not use
+ * {@link Autowired} constructor parameters
*
*
* @author Rod Johnson
diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java
index 9b3c26ac879..9b2bdd00b7a 100644
--- a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java
+++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java
@@ -4,19 +4,25 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
+import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.io.ClassPathResource;
import test.beans.Colour;
import test.beans.TestBean;
public class AutowiredConfigurationTests {
- public @Test
- void test() {
+
+ @Test
+ public void testAutowiredConfigurationDependencies() {
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
AutowiredConfigurationTests.class.getSimpleName() + ".xml", AutowiredConfigurationTests.class);
@@ -26,26 +32,49 @@ public class AutowiredConfigurationTests {
@Configuration
static class AutowiredConfig {
- private @Autowired
- Colour colour;
+ @Autowired
+ private Colour colour;
- public @Bean
- TestBean testBean() {
+ @Bean
+ public TestBean testBean() {
return new TestBean(colour.toString());
}
}
@Configuration
static class ColorConfig {
- public @Bean
- Colour colour() {
+
+ @Bean
+ public Colour colour() {
return Colour.RED;
}
}
+ /**
+ * {@link Autowired} constructors are not supported on {@link Configuration} classes
+ * due to CGLIB constraints
+ */
+ @Test(expected=BeanCreationException.class)
+ public void testAutowiredConfigurationConstructorsAreNotSupported() {
+ XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
+ GenericApplicationContext ctx = new GenericApplicationContext(factory);
+ ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
+ ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
+ ctx.refresh(); // should throw
+ }
- public @Test
- void testValueInjection() {
+ @Configuration
+ static class AutowiredConstructorConfig {
+ Colour colour;
+
+ @Autowired
+ AutowiredConstructorConfig(Colour colour) {
+ this.colour = colour;
+ }
+ }
+
+ @Test
+ public void testValueInjection() {
System.setProperty("myProp", "foo");
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
@@ -61,8 +90,8 @@ public class AutowiredConfigurationTests {
@Value("#{systemProperties.myProp}")
private String name = "default";
- public @Bean
- TestBean testBean() {
+ @Bean
+ public TestBean testBean() {
return new TestBean(name);
}
}
diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/annotation-config.xml b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/annotation-config.xml
new file mode 100644
index 00000000000..2a424dcbd60
--- /dev/null
+++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/annotation-config.xml
@@ -0,0 +1,9 @@
+
+
+
+
+