|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -16,24 +16,29 @@
@@ -16,24 +16,29 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.context.annotation.configuration; |
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
import javax.inject.Provider; |
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
|
import org.springframework.tests.sample.beans.Colour; |
|
|
|
|
import org.springframework.tests.sample.beans.TestBean; |
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanCreationException; |
|
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
|
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition; |
|
|
|
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
|
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
import org.springframework.context.annotation.Scope; |
|
|
|
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
|
|
|
|
import org.springframework.context.support.GenericApplicationContext; |
|
|
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
|
|
import org.springframework.tests.sample.beans.Colour; |
|
|
|
|
import org.springframework.tests.sample.beans.TestBean; |
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* System tests covering use of {@link Autowired} and {@link Value} within |
|
|
|
@ -53,32 +58,11 @@ public class AutowiredConfigurationTests {
@@ -53,32 +58,11 @@ public class AutowiredConfigurationTests {
|
|
|
|
|
assertThat(factory.getBean("testBean", TestBean.class).getName(), equalTo(Colour.RED.toString())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class AutowiredConfig { |
|
|
|
|
@Autowired |
|
|
|
|
private Colour colour; |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public TestBean testBean() { |
|
|
|
|
return new TestBean(colour.toString()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class ColorConfig { |
|
|
|
|
|
|
|
|
|
@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) |
|
|
|
|
@Test(expected = BeanCreationException.class) |
|
|
|
|
public void testAutowiredConfigurationConstructorsAreNotSupported() { |
|
|
|
|
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); |
|
|
|
|
new XmlBeanDefinitionReader(factory).loadBeanDefinitions( |
|
|
|
@ -89,22 +73,35 @@ public class AutowiredConfigurationTests {
@@ -89,22 +73,35 @@ public class AutowiredConfigurationTests {
|
|
|
|
|
ctx.refresh(); // should throw
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class AutowiredConstructorConfig { |
|
|
|
|
Colour colour; |
|
|
|
|
@Test |
|
|
|
|
public void testValueInjection() { |
|
|
|
|
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext( |
|
|
|
|
"ValueInjectionTests.xml", AutowiredConfigurationTests.class); |
|
|
|
|
doTestValueInjection(factory); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
AutowiredConstructorConfig(Colour colour) { |
|
|
|
|
this.colour = colour; |
|
|
|
|
} |
|
|
|
|
@Test |
|
|
|
|
public void testValueInjectionWithProviderFields() { |
|
|
|
|
AnnotationConfigApplicationContext factory = |
|
|
|
|
new AnnotationConfigApplicationContext(ValueConfigWithProviderFields.class); |
|
|
|
|
doTestValueInjection(factory); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testValueInjectionWithProviderConstructorArguments() { |
|
|
|
|
AnnotationConfigApplicationContext factory = |
|
|
|
|
new AnnotationConfigApplicationContext(ValueConfigWithProviderConstructorArguments.class); |
|
|
|
|
doTestValueInjection(factory); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testValueInjection() { |
|
|
|
|
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext( |
|
|
|
|
"ValueInjectionTests.xml", AutowiredConfigurationTests.class); |
|
|
|
|
public void testValueInjectionWithProviderMethodArguments() { |
|
|
|
|
AnnotationConfigApplicationContext factory = |
|
|
|
|
new AnnotationConfigApplicationContext(ValueConfigWithProviderMethodArguments.class); |
|
|
|
|
doTestValueInjection(factory); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void doTestValueInjection(BeanFactory factory) { |
|
|
|
|
System.clearProperty("myProp"); |
|
|
|
|
|
|
|
|
|
TestBean testBean = factory.getBean("testBean", TestBean.class); |
|
|
|
@ -130,6 +127,51 @@ public class AutowiredConfigurationTests {
@@ -130,6 +127,51 @@ public class AutowiredConfigurationTests {
|
|
|
|
|
assertNull(testBean.getName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testCustomProperties() { |
|
|
|
|
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext( |
|
|
|
|
"AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class); |
|
|
|
|
|
|
|
|
|
TestBean testBean = factory.getBean("testBean", TestBean.class); |
|
|
|
|
assertThat(testBean.getName(), equalTo("localhost")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class AutowiredConfig { |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private Colour colour; |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public TestBean testBean() { |
|
|
|
|
return new TestBean(colour.toString()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class ColorConfig { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public Colour colour() { |
|
|
|
|
return Colour.RED; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class AutowiredConstructorConfig { |
|
|
|
|
|
|
|
|
|
Colour colour; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
AutowiredConstructorConfig(Colour colour) { |
|
|
|
|
this.colour = colour; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class ValueConfig { |
|
|
|
|
|
|
|
|
@ -155,15 +197,71 @@ public class AutowiredConfigurationTests {
@@ -155,15 +197,71 @@ public class AutowiredConfigurationTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testCustomProperties() { |
|
|
|
|
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext( |
|
|
|
|
"AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class); |
|
|
|
|
@Configuration |
|
|
|
|
static class ValueConfigWithProviderFields { |
|
|
|
|
|
|
|
|
|
TestBean testBean = factory.getBean("testBean", TestBean.class); |
|
|
|
|
assertThat(testBean.getName(), equalTo("localhost")); |
|
|
|
|
@Value("#{systemProperties[myProp]}") |
|
|
|
|
private Provider<String> name; |
|
|
|
|
|
|
|
|
|
private Provider<String> name2; |
|
|
|
|
|
|
|
|
|
@Value("#{systemProperties[myProp]}") |
|
|
|
|
public void setName2(Provider<String> name) { |
|
|
|
|
this.name2 = name; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
|
public TestBean testBean() { |
|
|
|
|
return new TestBean(name.get()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
|
public TestBean testBean2() { |
|
|
|
|
return new TestBean(name2.get()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class ValueConfigWithProviderConstructorArguments { |
|
|
|
|
|
|
|
|
|
private final Provider<String> name; |
|
|
|
|
|
|
|
|
|
private final Provider<String> name2; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
public ValueConfigWithProviderConstructorArguments(@Value("#{systemProperties[myProp]}") Provider<String> name, |
|
|
|
|
@Value("#{systemProperties[myProp]}") Provider<String> name2) { |
|
|
|
|
this.name = name; |
|
|
|
|
this.name2 = name2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
|
public TestBean testBean() { |
|
|
|
|
return new TestBean(name.get()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
|
public TestBean testBean2() { |
|
|
|
|
return new TestBean(name2.get()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class ValueConfigWithProviderMethodArguments { |
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
|
public TestBean testBean(@Value("#{systemProperties[myProp]}") Provider<String> name) { |
|
|
|
|
return new TestBean(name.get()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
|
public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String> name2) { |
|
|
|
|
return new TestBean(name2.get()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class PropertiesConfig { |
|
|
|
|
|
|
|
|
|