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