1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .context .annotation .configuration ;
18
18
19
- import static org . hamcrest . CoreMatchers .* ;
20
- import static org . junit . Assert .*;
19
+ import javax . inject . Provider ;
20
+
21
21
import org .junit .Test ;
22
- import org .springframework .tests .sample .beans .Colour ;
23
- import org .springframework .tests .sample .beans .TestBean ;
24
22
25
23
import org .springframework .beans .factory .BeanCreationException ;
24
+ import org .springframework .beans .factory .BeanFactory ;
26
25
import org .springframework .beans .factory .annotation .Autowired ;
27
26
import org .springframework .beans .factory .annotation .Value ;
28
27
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
29
28
import org .springframework .beans .factory .support .RootBeanDefinition ;
30
29
import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
30
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
31
31
import org .springframework .context .annotation .Bean ;
32
32
import org .springframework .context .annotation .Configuration ;
33
33
import org .springframework .context .annotation .Scope ;
34
34
import org .springframework .context .support .ClassPathXmlApplicationContext ;
35
35
import org .springframework .context .support .GenericApplicationContext ;
36
36
import 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 .*;
37
42
38
43
/**
39
44
* System tests covering use of {@link Autowired} and {@link Value} within
@@ -53,32 +58,11 @@ public void testAutowiredConfigurationDependencies() {
53
58
assertThat (factory .getBean ("testBean" , TestBean .class ).getName (), equalTo (Colour .RED .toString ()));
54
59
}
55
60
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
-
77
61
/**
78
62
* {@link Autowired} constructors are not supported on {@link Configuration} classes
79
63
* due to CGLIB constraints
80
64
*/
81
- @ Test (expected = BeanCreationException .class )
65
+ @ Test (expected = BeanCreationException .class )
82
66
public void testAutowiredConfigurationConstructorsAreNotSupported () {
83
67
DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
84
68
new XmlBeanDefinitionReader (factory ).loadBeanDefinitions (
@@ -89,22 +73,35 @@ public void testAutowiredConfigurationConstructorsAreNotSupported() {
89
73
ctx .refresh (); // should throw
90
74
}
91
75
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
+ }
95
82
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 );
100
88
}
101
89
90
+ @ Test
91
+ public void testValueInjectionWithProviderConstructorArguments () {
92
+ AnnotationConfigApplicationContext factory =
93
+ new AnnotationConfigApplicationContext (ValueConfigWithProviderConstructorArguments .class );
94
+ doTestValueInjection (factory );
95
+ }
102
96
103
97
@ 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
+ }
107
103
104
+ private void doTestValueInjection (BeanFactory factory ) {
108
105
System .clearProperty ("myProp" );
109
106
110
107
TestBean testBean = factory .getBean ("testBean" , TestBean .class );
@@ -130,6 +127,51 @@ public void testValueInjection() {
130
127
assertNull (testBean .getName ());
131
128
}
132
129
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
+
133
175
@ Configuration
134
176
static class ValueConfig {
135
177
@@ -155,15 +197,71 @@ public TestBean testBean2() {
155
197
}
156
198
157
199
158
- @ Test
159
- public void testCustomProperties () {
160
- ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
161
- "AutowiredConfigurationTests-custom.xml" , AutowiredConfigurationTests .class );
200
+ @ Configuration
201
+ static class ValueConfigWithProviderFields {
162
202
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
+ }
165
222
}
166
223
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
+
167
265
@ Configuration
168
266
static class PropertiesConfig {
169
267
0 commit comments