|
32 | 32 | import java.util.LinkedHashSet;
|
33 | 33 | import java.util.List;
|
34 | 34 | import java.util.Map;
|
| 35 | +import java.util.Optional; |
35 | 36 | import java.util.Properties;
|
36 | 37 | import java.util.Set;
|
37 | 38 | import java.util.SortedMap;
|
@@ -2600,6 +2601,26 @@ void factoryBeanSelfInjectionViaFactoryMethod() {
|
2600 | 2601 | assertThat(bean.testBean).isSameAs(bf.getBean("annotatedBean"));
|
2601 | 2602 | }
|
2602 | 2603 |
|
| 2604 | + @Test |
| 2605 | + public void mixedNullableArgMethodInjection(){ |
| 2606 | + bf.registerSingleton("nonNullBean", "Test"); |
| 2607 | + bf.registerBeanDefinition("mixedNullableInjectionBean", |
| 2608 | + new RootBeanDefinition(MixedNullableInjectionBean.class)); |
| 2609 | + MixedNullableInjectionBean mixedNullableInjectionBean = bf.getBean(MixedNullableInjectionBean.class); |
| 2610 | + assertThat(mixedNullableInjectionBean.nonNullBean).isNotNull(); |
| 2611 | + assertThat(mixedNullableInjectionBean.nullableBean).isNull(); |
| 2612 | + } |
| 2613 | + |
| 2614 | + @Test |
| 2615 | + public void mixedOptionalArgMethodInjection(){ |
| 2616 | + bf.registerSingleton("nonNullBean", "Test"); |
| 2617 | + bf.registerBeanDefinition("mixedOptionalInjectionBean", |
| 2618 | + new RootBeanDefinition(MixedOptionalInjectionBean.class)); |
| 2619 | + MixedOptionalInjectionBean mixedOptionalInjectionBean = bf.getBean(MixedOptionalInjectionBean.class); |
| 2620 | + assertThat(mixedOptionalInjectionBean.nonNullBean).isNotNull(); |
| 2621 | + assertThat(mixedOptionalInjectionBean.nullableBean).isNull(); |
| 2622 | + } |
| 2623 | + |
2603 | 2624 | private <E extends UnsatisfiedDependencyException> Consumer<E> methodParameterDeclaredOn(
|
2604 | 2625 | Class<?> expected) {
|
2605 | 2626 | return declaredOn(
|
@@ -4346,4 +4367,34 @@ public static TestBean newTestBean2() {
|
4346 | 4367 | }
|
4347 | 4368 | }
|
4348 | 4369 |
|
| 4370 | + |
| 4371 | + static class MixedNullableInjectionBean { |
| 4372 | + |
| 4373 | + @Nullable |
| 4374 | + public Integer nullableBean; |
| 4375 | + |
| 4376 | + public String nonNullBean; |
| 4377 | + |
| 4378 | + @Autowired(required = false) |
| 4379 | + public void nullabilityInjection(@Nullable Integer nullableBean, String nonNullBean) { |
| 4380 | + this.nullableBean = nullableBean; |
| 4381 | + this.nonNullBean = nonNullBean; |
| 4382 | + } |
| 4383 | + } |
| 4384 | + |
| 4385 | + |
| 4386 | + static class MixedOptionalInjectionBean { |
| 4387 | + |
| 4388 | + @Nullable |
| 4389 | + public Integer nullableBean; |
| 4390 | + |
| 4391 | + public String nonNullBean; |
| 4392 | + |
| 4393 | + @Autowired(required = false) |
| 4394 | + public void optionalInjection(Optional<Integer> optionalBean, String nonNullBean) { |
| 4395 | + optionalBean.ifPresent(bean -> this.nullableBean = bean); |
| 4396 | + this.nonNullBean = nonNullBean; |
| 4397 | + } |
| 4398 | + } |
| 4399 | + |
4349 | 4400 | }
|
0 commit comments