Browse Source

RESOLVED - issue SPR-5596: Test @Autowired @Configuration class constructors

conversation
Chris Beams 16 years ago
parent
commit
aee7f1ce53
  1. 4
      org.springframework.context/src/main/java/org/springframework/context/annotation/Configuration.java
  2. 53
      org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java
  3. 9
      org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/annotation-config.xml

4
org.springframework.context/src/main/java/org/springframework/context/annotation/Configuration.java

@ -45,8 +45,8 @@ import org.springframework.stereotype.Component; @@ -45,8 +45,8 @@ import org.springframework.stereotype.Component;
* <ul>
* <li>Configuration classes must be non-final
* <li>Configuration classes must be non-local (may not be declared within a method)
* <li>Configuration classes must have a default/no-arg constructor or at least one
* {@link Autowired} constructor
* <li>Configuration classes must have a default/no-arg constructor and may not use
* {@link Autowired} constructor parameters
* </ul>
*
* @author Rod Johnson

53
org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

@ -4,19 +4,25 @@ import static org.hamcrest.CoreMatchers.*; @@ -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 { @@ -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
}
@Configuration
static class AutowiredConstructorConfig {
Colour colour;
@Autowired
AutowiredConstructorConfig(Colour colour) {
this.colour = colour;
}
}
public @Test
void testValueInjection() {
@Test
public void testValueInjection() {
System.setProperty("myProp", "foo");
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
@ -61,8 +90,8 @@ public class AutowiredConfigurationTests { @@ -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);
}
}

9
org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/annotation-config.xml

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config/>
</beans>
Loading…
Cancel
Save