11/*
2- * Copyright 2002-2013 the original author or authors.
2+ * Copyright 2002-2014 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .context .annotation .configuration ;
1818
19- import static org . hamcrest . CoreMatchers .* ;
20- import static org . junit . Assert .*;
19+ import javax . inject . Provider ;
20+
2121import org .junit .Test ;
22- import org .springframework .tests .sample .beans .Colour ;
23- import org .springframework .tests .sample .beans .TestBean ;
2422
2523import org .springframework .beans .factory .BeanCreationException ;
24+ import org .springframework .beans .factory .BeanFactory ;
2625import org .springframework .beans .factory .annotation .Autowired ;
2726import org .springframework .beans .factory .annotation .Value ;
2827import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
2928import org .springframework .beans .factory .support .RootBeanDefinition ;
3029import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
30+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
3131import org .springframework .context .annotation .Bean ;
3232import org .springframework .context .annotation .Configuration ;
3333import org .springframework .context .annotation .Scope ;
3434import org .springframework .context .support .ClassPathXmlApplicationContext ;
3535import org .springframework .context .support .GenericApplicationContext ;
3636import org .springframework .core .io .ClassPathResource ;
37+ import org .springframework .tests .sample .beans .Colour ;
38+ import org .springframework .tests .sample .beans .TestBean ;
39+
40+ import static org .hamcrest .CoreMatchers .*;
41+ import static org .junit .Assert .*;
3742
3843/**
3944 * System tests covering use of {@link Autowired} and {@link Value} within
@@ -53,32 +58,11 @@ public void testAutowiredConfigurationDependencies() {
5358 assertThat (factory .getBean ("testBean" , TestBean .class ).getName (), equalTo (Colour .RED .toString ()));
5459 }
5560
56- @ Configuration
57- static class AutowiredConfig {
58- @ Autowired
59- private Colour colour ;
60-
61- @ Bean
62- public TestBean testBean () {
63- return new TestBean (colour .toString ());
64- }
65- }
66-
67- @ Configuration
68- static class ColorConfig {
69-
70- @ Bean
71- public Colour colour () {
72- return Colour .RED ;
73- }
74- }
75-
76-
7761 /**
7862 * {@link Autowired} constructors are not supported on {@link Configuration} classes
7963 * due to CGLIB constraints
8064 */
81- @ Test (expected = BeanCreationException .class )
65+ @ Test (expected = BeanCreationException .class )
8266 public void testAutowiredConfigurationConstructorsAreNotSupported () {
8367 DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
8468 new XmlBeanDefinitionReader (factory ).loadBeanDefinitions (
@@ -89,22 +73,35 @@ public void testAutowiredConfigurationConstructorsAreNotSupported() {
8973 ctx .refresh (); // should throw
9074 }
9175
92- @ Configuration
93- static class AutowiredConstructorConfig {
94- Colour colour ;
76+ @ Test
77+ public void testValueInjection () {
78+ ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
79+ "ValueInjectionTests.xml" , AutowiredConfigurationTests .class );
80+ doTestValueInjection (factory );
81+ }
9582
96- @ Autowired
97- AutowiredConstructorConfig (Colour colour ) {
98- this .colour = colour ;
99- }
83+ @ Test
84+ public void testValueInjectionWithProviderFields () {
85+ AnnotationConfigApplicationContext factory =
86+ new AnnotationConfigApplicationContext (ValueConfigWithProviderFields .class );
87+ doTestValueInjection (factory );
10088 }
10189
90+ @ Test
91+ public void testValueInjectionWithProviderConstructorArguments () {
92+ AnnotationConfigApplicationContext factory =
93+ new AnnotationConfigApplicationContext (ValueConfigWithProviderConstructorArguments .class );
94+ doTestValueInjection (factory );
95+ }
10296
10397 @ Test
104- public void testValueInjection () {
105- ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
106- "ValueInjectionTests.xml" , AutowiredConfigurationTests .class );
98+ public void testValueInjectionWithProviderMethodArguments () {
99+ AnnotationConfigApplicationContext factory =
100+ new AnnotationConfigApplicationContext (ValueConfigWithProviderMethodArguments .class );
101+ doTestValueInjection (factory );
102+ }
107103
104+ private void doTestValueInjection (BeanFactory factory ) {
108105 System .clearProperty ("myProp" );
109106
110107 TestBean testBean = factory .getBean ("testBean" , TestBean .class );
@@ -130,6 +127,51 @@ public void testValueInjection() {
130127 assertNull (testBean .getName ());
131128 }
132129
130+ @ Test
131+ public void testCustomProperties () {
132+ ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
133+ "AutowiredConfigurationTests-custom.xml" , AutowiredConfigurationTests .class );
134+
135+ TestBean testBean = factory .getBean ("testBean" , TestBean .class );
136+ assertThat (testBean .getName (), equalTo ("localhost" ));
137+ }
138+
139+
140+ @ Configuration
141+ static class AutowiredConfig {
142+
143+ @ Autowired
144+ private Colour colour ;
145+
146+ @ Bean
147+ public TestBean testBean () {
148+ return new TestBean (colour .toString ());
149+ }
150+ }
151+
152+
153+ @ Configuration
154+ static class ColorConfig {
155+
156+ @ Bean
157+ public Colour colour () {
158+ return Colour .RED ;
159+ }
160+ }
161+
162+
163+ @ Configuration
164+ static class AutowiredConstructorConfig {
165+
166+ Colour colour ;
167+
168+ @ Autowired
169+ AutowiredConstructorConfig (Colour colour ) {
170+ this .colour = colour ;
171+ }
172+ }
173+
174+
133175 @ Configuration
134176 static class ValueConfig {
135177
@@ -155,15 +197,71 @@ public TestBean testBean2() {
155197 }
156198
157199
158- @ Test
159- public void testCustomProperties () {
160- ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
161- "AutowiredConfigurationTests-custom.xml" , AutowiredConfigurationTests .class );
200+ @ Configuration
201+ static class ValueConfigWithProviderFields {
162202
163- TestBean testBean = factory .getBean ("testBean" , TestBean .class );
164- assertThat (testBean .getName (), equalTo ("localhost" ));
203+ @ Value ("#{systemProperties[myProp]}" )
204+ private Provider <String > name ;
205+
206+ private Provider <String > name2 ;
207+
208+ @ Value ("#{systemProperties[myProp]}" )
209+ public void setName2 (Provider <String > name ) {
210+ this .name2 = name ;
211+ }
212+
213+ @ Bean @ Scope ("prototype" )
214+ public TestBean testBean () {
215+ return new TestBean (name .get ());
216+ }
217+
218+ @ Bean @ Scope ("prototype" )
219+ public TestBean testBean2 () {
220+ return new TestBean (name2 .get ());
221+ }
165222 }
166223
224+
225+ static class ValueConfigWithProviderConstructorArguments {
226+
227+ private final Provider <String > name ;
228+
229+ private final Provider <String > name2 ;
230+
231+ @ Autowired
232+ public ValueConfigWithProviderConstructorArguments (@ Value ("#{systemProperties[myProp]}" ) Provider <String > name ,
233+ @ Value ("#{systemProperties[myProp]}" ) Provider <String > name2 ) {
234+ this .name = name ;
235+ this .name2 = name2 ;
236+ }
237+
238+ @ Bean @ Scope ("prototype" )
239+ public TestBean testBean () {
240+ return new TestBean (name .get ());
241+ }
242+
243+ @ Bean @ Scope ("prototype" )
244+ public TestBean testBean2 () {
245+ return new TestBean (name2 .get ());
246+ }
247+ }
248+
249+
250+ @ Configuration
251+ static class ValueConfigWithProviderMethodArguments {
252+
253+ @ Bean @ Scope ("prototype" )
254+ public TestBean testBean (@ Value ("#{systemProperties[myProp]}" ) Provider <String > name ) {
255+ return new TestBean (name .get ());
256+ }
257+
258+ @ Bean @ Scope ("prototype" )
259+ public TestBean testBean2 (@ Value ("#{systemProperties[myProp]}" ) Provider <String > name2 ) {
260+ return new TestBean (name2 .get ());
261+ }
262+ }
263+
264+
167265 @ Configuration
168266 static class PropertiesConfig {
169267
0 commit comments