|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2009 the original author or authors. |
|
|
|
* Copyright 2002-2010 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -18,8 +18,10 @@ package org.springframework.context.annotation.configuration; |
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import test.beans.Colour; |
|
|
|
|
|
|
|
import test.beans.TestBean; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanCreationException; |
|
|
|
import org.springframework.beans.factory.BeanCreationException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
@ -27,18 +29,17 @@ import org.springframework.beans.factory.support.RootBeanDefinition; |
|
|
|
import org.springframework.beans.factory.xml.XmlBeanFactory; |
|
|
|
import org.springframework.beans.factory.xml.XmlBeanFactory; |
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
|
|
import org.springframework.context.annotation.Scope; |
|
|
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
|
|
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
|
|
|
import org.springframework.context.support.GenericApplicationContext; |
|
|
|
import org.springframework.context.support.GenericApplicationContext; |
|
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
|
|
|
|
|
|
|
|
|
import test.beans.Colour; |
|
|
|
|
|
|
|
import test.beans.TestBean; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* System tests covering use of {@link Autowired} and {@link Value} within |
|
|
|
* System tests covering use of {@link Autowired} and {@link Value} within |
|
|
|
* {@link Configuration} classes. |
|
|
|
* {@link Configuration} classes. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Chris Beams |
|
|
|
* @author Chris Beams |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class AutowiredConfigurationTests { |
|
|
|
public class AutowiredConfigurationTests { |
|
|
|
|
|
|
|
|
|
|
|
@ -98,25 +99,56 @@ public class AutowiredConfigurationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testValueInjection() { |
|
|
|
public void testValueInjection() { |
|
|
|
System.setProperty("myProp", "foo"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext( |
|
|
|
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext( |
|
|
|
"ValueInjectionTests.xml", AutowiredConfigurationTests.class); |
|
|
|
"ValueInjectionTests.xml", AutowiredConfigurationTests.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.clearProperty("myProp"); |
|
|
|
|
|
|
|
|
|
|
|
TestBean testBean = factory.getBean("testBean", TestBean.class); |
|
|
|
TestBean testBean = factory.getBean("testBean", TestBean.class); |
|
|
|
|
|
|
|
assertNull(testBean.getName()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
testBean = factory.getBean("testBean2", TestBean.class); |
|
|
|
|
|
|
|
assertNull(testBean.getName()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.setProperty("myProp", "foo"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
testBean = factory.getBean("testBean", TestBean.class); |
|
|
|
|
|
|
|
assertThat(testBean.getName(), equalTo("foo")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
testBean = factory.getBean("testBean2", TestBean.class); |
|
|
|
assertThat(testBean.getName(), equalTo("foo")); |
|
|
|
assertThat(testBean.getName(), equalTo("foo")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.clearProperty("myProp"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
testBean = factory.getBean("testBean", TestBean.class); |
|
|
|
|
|
|
|
assertNull(testBean.getName()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
testBean = factory.getBean("testBean2", TestBean.class); |
|
|
|
|
|
|
|
assertNull(testBean.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
static class ValueConfig { |
|
|
|
static class ValueConfig { |
|
|
|
|
|
|
|
|
|
|
|
@Value("#{systemProperties.myProp}") |
|
|
|
@Value("#{systemProperties[myProp]}") |
|
|
|
private String name = "default"; |
|
|
|
private String name; |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
private String name2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("#{systemProperties[myProp]}") |
|
|
|
|
|
|
|
public void setName2(String name) { |
|
|
|
|
|
|
|
this.name2 = name; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
public TestBean testBean() { |
|
|
|
public TestBean testBean() { |
|
|
|
return new TestBean(name); |
|
|
|
return new TestBean(name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean @Scope("prototype") |
|
|
|
|
|
|
|
public TestBean testBean2() { |
|
|
|
|
|
|
|
return new TestBean(name2); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|