1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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 java .io .IOException ;
19
20
import java .util .List ;
20
21
import java .util .Optional ;
21
22
import javax .inject .Provider ;
36
37
import org .springframework .context .support .ClassPathXmlApplicationContext ;
37
38
import org .springframework .context .support .GenericApplicationContext ;
38
39
import org .springframework .core .io .ClassPathResource ;
40
+ import org .springframework .core .io .Resource ;
39
41
import org .springframework .tests .sample .beans .Colour ;
40
42
import org .springframework .tests .sample .beans .TestBean ;
41
43
@@ -53,38 +55,38 @@ public class AutowiredConfigurationTests {
53
55
54
56
@ Test
55
57
public void testAutowiredConfigurationDependencies () {
56
- ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
58
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
57
59
AutowiredConfigurationTests .class .getSimpleName () + ".xml" , AutowiredConfigurationTests .class );
58
60
59
- assertThat (factory .getBean ("colour" , Colour .class ), equalTo (Colour .RED ));
60
- assertThat (factory .getBean ("testBean" , TestBean .class ).getName (), equalTo (Colour .RED .toString ()));
61
+ assertThat (context .getBean ("colour" , Colour .class ), equalTo (Colour .RED ));
62
+ assertThat (context .getBean ("testBean" , TestBean .class ).getName (), equalTo (Colour .RED .toString ()));
61
63
}
62
64
63
65
@ Test
64
66
public void testAutowiredConfigurationMethodDependencies () {
65
- AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext (
67
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
66
68
AutowiredMethodConfig .class , ColorConfig .class );
67
69
68
- assertThat (factory .getBean (Colour .class ), equalTo (Colour .RED ));
69
- assertThat (factory .getBean (TestBean .class ).getName (), equalTo ("RED-RED" ));
70
+ assertThat (context .getBean (Colour .class ), equalTo (Colour .RED ));
71
+ assertThat (context .getBean (TestBean .class ).getName (), equalTo ("RED-RED" ));
70
72
}
71
73
72
74
@ Test
73
75
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndAvailable () {
74
- AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext (
76
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
75
77
OptionalAutowiredMethodConfig .class , ColorConfig .class );
76
78
77
- assertThat (factory .getBean (Colour .class ), equalTo (Colour .RED ));
78
- assertThat (factory .getBean (TestBean .class ).getName (), equalTo ("RED-RED" ));
79
+ assertThat (context .getBean (Colour .class ), equalTo (Colour .RED ));
80
+ assertThat (context .getBean (TestBean .class ).getName (), equalTo ("RED-RED" ));
79
81
}
80
82
81
83
@ Test
82
84
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable () {
83
- AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext (
85
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
84
86
OptionalAutowiredMethodConfig .class );
85
87
86
- assertTrue (factory .getBeansOfType (Colour .class ).isEmpty ());
87
- assertThat (factory .getBean (TestBean .class ).getName (), equalTo ("" ));
88
+ assertTrue (context .getBeansOfType (Colour .class ).isEmpty ());
89
+ assertThat (context .getBean (TestBean .class ).getName (), equalTo ("" ));
88
90
}
89
91
90
92
/**
@@ -93,76 +95,90 @@ public void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvaila
93
95
*/
94
96
@ Test (expected = BeanCreationException .class )
95
97
public void testAutowiredConfigurationConstructorsAreNotSupported () {
96
- DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
97
- new XmlBeanDefinitionReader (factory ).loadBeanDefinitions (
98
+ DefaultListableBeanFactory context = new DefaultListableBeanFactory ();
99
+ new XmlBeanDefinitionReader (context ).loadBeanDefinitions (
98
100
new ClassPathResource ("annotation-config.xml" , AutowiredConstructorConfig .class ));
99
- GenericApplicationContext ctx = new GenericApplicationContext (factory );
101
+ GenericApplicationContext ctx = new GenericApplicationContext (context );
100
102
ctx .registerBeanDefinition ("config1" , new RootBeanDefinition (AutowiredConstructorConfig .class ));
101
103
ctx .registerBeanDefinition ("config2" , new RootBeanDefinition (ColorConfig .class ));
102
104
ctx .refresh (); // should throw
103
105
}
104
106
105
107
@ Test
106
108
public void testValueInjection () {
107
- ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
109
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
108
110
"ValueInjectionTests.xml" , AutowiredConfigurationTests .class );
109
- doTestValueInjection (factory );
111
+ doTestValueInjection (context );
110
112
}
111
113
112
114
@ Test
113
115
public void testValueInjectionWithProviderFields () {
114
- AnnotationConfigApplicationContext factory =
116
+ AnnotationConfigApplicationContext context =
115
117
new AnnotationConfigApplicationContext (ValueConfigWithProviderFields .class );
116
- doTestValueInjection (factory );
118
+ doTestValueInjection (context );
117
119
}
118
120
119
121
@ Test
120
122
public void testValueInjectionWithProviderConstructorArguments () {
121
- AnnotationConfigApplicationContext factory =
123
+ AnnotationConfigApplicationContext context =
122
124
new AnnotationConfigApplicationContext (ValueConfigWithProviderConstructorArguments .class );
123
- doTestValueInjection (factory );
125
+ doTestValueInjection (context );
124
126
}
125
127
126
128
@ Test
127
129
public void testValueInjectionWithProviderMethodArguments () {
128
- AnnotationConfigApplicationContext factory =
130
+ AnnotationConfigApplicationContext context =
129
131
new AnnotationConfigApplicationContext (ValueConfigWithProviderMethodArguments .class );
130
- doTestValueInjection (factory );
132
+ doTestValueInjection (context );
131
133
}
132
134
133
- private void doTestValueInjection (BeanFactory factory ) {
135
+ private void doTestValueInjection (BeanFactory context ) {
134
136
System .clearProperty ("myProp" );
135
137
136
- TestBean testBean = factory .getBean ("testBean" , TestBean .class );
138
+ TestBean testBean = context .getBean ("testBean" , TestBean .class );
137
139
assertNull (testBean .getName ());
138
140
139
- testBean = factory .getBean ("testBean2" , TestBean .class );
141
+ testBean = context .getBean ("testBean2" , TestBean .class );
140
142
assertNull (testBean .getName ());
141
143
142
144
System .setProperty ("myProp" , "foo" );
143
145
144
- testBean = factory .getBean ("testBean" , TestBean .class );
146
+ testBean = context .getBean ("testBean" , TestBean .class );
145
147
assertThat (testBean .getName (), equalTo ("foo" ));
146
148
147
- testBean = factory .getBean ("testBean2" , TestBean .class );
149
+ testBean = context .getBean ("testBean2" , TestBean .class );
148
150
assertThat (testBean .getName (), equalTo ("foo" ));
149
151
150
152
System .clearProperty ("myProp" );
151
153
152
- testBean = factory .getBean ("testBean" , TestBean .class );
154
+ testBean = context .getBean ("testBean" , TestBean .class );
153
155
assertNull (testBean .getName ());
154
156
155
- testBean = factory .getBean ("testBean2" , TestBean .class );
157
+ testBean = context .getBean ("testBean2" , TestBean .class );
156
158
assertNull (testBean .getName ());
157
159
}
158
160
159
161
@ Test
160
- public void testCustomProperties () {
161
- ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext (
162
+ public void testCustomPropertiesWithClassPathContext () throws IOException {
163
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
162
164
"AutowiredConfigurationTests-custom.xml" , AutowiredConfigurationTests .class );
163
165
164
- TestBean testBean = factory .getBean ("testBean" , TestBean .class );
166
+ TestBean testBean = context .getBean ("testBean" , TestBean .class );
165
167
assertThat (testBean .getName (), equalTo ("localhost" ));
168
+ assertThat (testBean .getAge (), equalTo ((int ) new ClassPathResource ("log4j.properties" ).contentLength ()));
169
+ }
170
+
171
+ @ Test
172
+ public void testCustomPropertiesWithGenericContext () throws IOException {
173
+ GenericApplicationContext context = new GenericApplicationContext ();
174
+ // context.setResourceLoader(new FileSystemResourceLoader());
175
+ new XmlBeanDefinitionReader (context ).loadBeanDefinitions (
176
+ new ClassPathResource ("AutowiredConfigurationTests-custom.xml" , AutowiredConfigurationTests .class ));
177
+ context .refresh ();
178
+
179
+ TestBean testBean = context .getBean ("testBean" , TestBean .class );
180
+ assertThat (testBean .getName (), equalTo ("localhost" ));
181
+ assertThat (testBean .getAge (), equalTo ((int ) new ClassPathResource ("log4j.properties" ).contentLength ()));
166
182
}
167
183
168
184
@@ -321,14 +337,21 @@ static class PropertiesConfig {
321
337
322
338
private String hostname ;
323
339
340
+ private Resource resource ;
341
+
324
342
@ Value ("#{myProps.hostname}" )
325
343
public void setHostname (String hostname ) {
326
344
this .hostname = hostname ;
327
345
}
328
346
347
+ @ Value ("log4j.properties" )
348
+ public void setResource (Resource resource ) {
349
+ this .resource = resource ;
350
+ }
351
+
329
352
@ Bean
330
- public TestBean testBean () {
331
- return new TestBean (hostname );
353
+ public TestBean testBean () throws IOException {
354
+ return new TestBean (hostname , ( int ) resource . contentLength () );
332
355
}
333
356
}
334
357
0 commit comments