1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 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.
26
26
import org .junit .jupiter .api .Test ;
27
27
28
28
import org .springframework .beans .factory .BeanFactory ;
29
+ import org .springframework .beans .factory .InitializingBean ;
29
30
import org .springframework .beans .factory .ObjectFactory ;
30
31
import org .springframework .beans .factory .annotation .Autowired ;
31
32
import org .springframework .beans .factory .annotation .Value ;
43
44
import org .springframework .core .annotation .AliasFor ;
44
45
import org .springframework .core .io .ClassPathResource ;
45
46
import org .springframework .core .io .Resource ;
47
+ import org .springframework .util .Assert ;
46
48
47
49
import static org .assertj .core .api .Assertions .assertThat ;
48
50
@@ -91,7 +93,7 @@ void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
91
93
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
92
94
OptionalAutowiredMethodConfig .class );
93
95
94
- assertThat (context .getBeansOfType (Colour .class ). isEmpty ()). isTrue ();
96
+ assertThat (context .getBeansOfType (Colour .class )). isEmpty ();
95
97
assertThat (context .getBean (TestBean .class ).getName ()).isEmpty ();
96
98
context .close ();
97
99
}
@@ -183,14 +185,22 @@ void testValueInjectionWithProviderMethodArguments() {
183
185
context .close ();
184
186
}
185
187
188
+ @ Test
189
+ void testValueInjectionWithAccidentalAutowiredAnnotations () {
190
+ AnnotationConfigApplicationContext context =
191
+ new AnnotationConfigApplicationContext (ValueConfigWithAccidentalAutowiredAnnotations .class );
192
+ doTestValueInjection (context );
193
+ context .close ();
194
+ }
195
+
186
196
private void doTestValueInjection (BeanFactory context ) {
187
197
System .clearProperty ("myProp" );
188
198
189
199
TestBean testBean = context .getBean ("testBean" , TestBean .class );
190
- assertThat (( Object ) testBean .getName ()).isNull ();
200
+ assertThat (testBean .getName ()).isNull ();
191
201
192
202
testBean = context .getBean ("testBean2" , TestBean .class );
193
- assertThat (( Object ) testBean .getName ()).isNull ();
203
+ assertThat (testBean .getName ()).isNull ();
194
204
195
205
System .setProperty ("myProp" , "foo" );
196
206
@@ -203,10 +213,10 @@ private void doTestValueInjection(BeanFactory context) {
203
213
System .clearProperty ("myProp" );
204
214
205
215
testBean = context .getBean ("testBean" , TestBean .class );
206
- assertThat (( Object ) testBean .getName ()).isNull ();
216
+ assertThat (testBean .getName ()).isNull ();
207
217
208
218
testBean = context .getBean ("testBean2" , TestBean .class );
209
- assertThat (( Object ) testBean .getName ()).isNull ();
219
+ assertThat (testBean .getName ()).isNull ();
210
220
}
211
221
212
222
@ Test
@@ -281,7 +291,7 @@ public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours
281
291
return new TestBean ("" );
282
292
}
283
293
else {
284
- return new TestBean (colour .get (). toString () + "-" + colours .get ().get (0 ).toString ());
294
+ return new TestBean (colour .get () + "-" + colours .get ().get (0 ).toString ());
285
295
}
286
296
}
287
297
}
@@ -494,6 +504,32 @@ public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String>
494
504
}
495
505
496
506
507
+ @ Configuration
508
+ static class ValueConfigWithAccidentalAutowiredAnnotations implements InitializingBean {
509
+
510
+ boolean invoked ;
511
+
512
+ @ Override
513
+ public void afterPropertiesSet () {
514
+ Assert .state (!invoked , "Factory method must not get invoked on startup" );
515
+ }
516
+
517
+ @ Bean @ Scope ("prototype" )
518
+ @ Autowired
519
+ public TestBean testBean (@ Value ("#{systemProperties[myProp]}" ) Provider <String > name ) {
520
+ invoked = true ;
521
+ return new TestBean (name .get ());
522
+ }
523
+
524
+ @ Bean @ Scope ("prototype" )
525
+ @ Autowired
526
+ public TestBean testBean2 (@ Value ("#{systemProperties[myProp]}" ) Provider <String > name2 ) {
527
+ invoked = true ;
528
+ return new TestBean (name2 .get ());
529
+ }
530
+ }
531
+
532
+
497
533
@ Configuration
498
534
static class PropertiesConfig {
499
535
0 commit comments