|
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 |
|
@@ -183,6 +185,14 @@ 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 |
|
@@ -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