@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2009 the original author or authors .
* Copyright 2002 - 201 0 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 .
@ -18,8 +18,10 @@ package org.springframework.context.annotation.configuration;
@@ -18,8 +18,10 @@ package org.springframework.context.annotation.configuration;
import static org.hamcrest.CoreMatchers.* ;
import static org.junit.Assert.* ;
import org.junit.Test ;
import test.beans.Colour ;
import test.beans.TestBean ;
import org.springframework.beans.factory.BeanCreationException ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
@ -27,18 +29,17 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -27,18 +29,17 @@ 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.annotation.Scope ;
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 ;
/ * *
* System tests covering use of { @link Autowired } and { @link Value } within
* { @link Configuration } classes .
*
* @author Chris Beams
* @author Juergen Hoeller
* /
public class AutowiredConfigurationTests {
@ -98,25 +99,56 @@ public class AutowiredConfigurationTests {
@@ -98,25 +99,56 @@ public class AutowiredConfigurationTests {
@Test
public void testValueInjection ( ) {
System . setProperty ( "myProp" , "foo" ) ;
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
"ValueInjectionTests.xml" , AutowiredConfigurationTests . class ) ;
System . clearProperty ( "myProp" ) ;
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" ) ) ;
System . clearProperty ( "myProp" ) ;
testBean = factory . getBean ( "testBean" , TestBean . class ) ;
assertNull ( testBean . getName ( ) ) ;
testBean = factory . getBean ( "testBean2" , TestBean . class ) ;
assertNull ( testBean . getName ( ) ) ;
}
@Configuration
static class ValueConfig {
@Value ( "#{systemProperties.myProp}" )
private String name = "default" ;
@Value ( "#{systemProperties[myProp] }" )
private String name ;
@Bean
private String name2 ;
@Value ( "#{systemProperties[myProp]}" )
public void setName2 ( String name ) {
this . name2 = name ;
}
@Bean @Scope ( "prototype" )
public TestBean testBean ( ) {
return new TestBean ( name ) ;
}
@Bean @Scope ( "prototype" )
public TestBean testBean2 ( ) {
return new TestBean ( name2 ) ;
}
}