|
|
|
@ -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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|