RESOLVED - issue SPR-5596: Test @Autowired @Configuration class constructors
This commit is contained in:
parent
1697932c57
commit
aee7f1ce53
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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…
Reference in New Issue