Polish "Improve @Autowired method injection on mixed nullability args"

See gh-17215
This commit is contained in:
Stephane Nicoll 2023-08-25 12:45:44 +02:00
parent 32c0540424
commit 8efc7a958e
1 changed files with 22 additions and 23 deletions

View File

@ -2602,20 +2602,20 @@ public class AutowiredAnnotationBeanPostProcessorTests {
} }
@Test @Test
public void testMethodInjectionWithMultiMixedNullableArgs(){ public void mixedNullableArgMethodInjection(){
bf.registerBeanDefinition("nonNullBean", new RootBeanDefinition( bf.registerSingleton("nonNullBean", "Test");
NonNullBean.class)); bf.registerBeanDefinition("mixedNullableInjectionBean",
bf.registerBeanDefinition("mixedNullableInjectionBean", new RootBeanDefinition(MixedNullableInjectionBean.class)); new RootBeanDefinition(MixedNullableInjectionBean.class));
MixedNullableInjectionBean mixedNullableInjectionBean = bf.getBean(MixedNullableInjectionBean.class); MixedNullableInjectionBean mixedNullableInjectionBean = bf.getBean(MixedNullableInjectionBean.class);
assertThat(mixedNullableInjectionBean.nonNullBean).isNotNull(); assertThat(mixedNullableInjectionBean.nonNullBean).isNotNull();
assertThat(mixedNullableInjectionBean.nullableBean).isNull(); assertThat(mixedNullableInjectionBean.nullableBean).isNull();
} }
@Test @Test
public void testMethodInjectionWithMultiMixedOptionalArgs(){ public void mixedOptionalArgMethodInjection(){
bf.registerBeanDefinition("nonNullBean", new RootBeanDefinition( bf.registerSingleton("nonNullBean", "Test");
NonNullBean.class)); bf.registerBeanDefinition("mixedOptionalInjectionBean",
bf.registerBeanDefinition("mixedOptionalInjectionBean", new RootBeanDefinition(MixedOptionalInjectionBean.class)); new RootBeanDefinition(MixedOptionalInjectionBean.class));
MixedOptionalInjectionBean mixedOptionalInjectionBean = bf.getBean(MixedOptionalInjectionBean.class); MixedOptionalInjectionBean mixedOptionalInjectionBean = bf.getBean(MixedOptionalInjectionBean.class);
assertThat(mixedOptionalInjectionBean.nonNullBean).isNotNull(); assertThat(mixedOptionalInjectionBean.nonNullBean).isNotNull();
assertThat(mixedOptionalInjectionBean.nullableBean).isNull(); assertThat(mixedOptionalInjectionBean.nullableBean).isNull();
@ -4367,32 +4367,31 @@ public class AutowiredAnnotationBeanPostProcessorTests {
} }
} }
static class NullableBean {
} static class MixedNullableInjectionBean {
static class NonNullBean {
} @Nullable
public Integer nullableBean;
static class MixedNullableInjectionBean{ public String nonNullBean;
public NonNullBean nonNullBean;
public NullableBean nullableBean;
@Autowired(required = false) @Autowired(required = false)
public void nullabilityInjection(@Nullable NullableBean nullableBean, NonNullBean nonNullBean){ public void nullabilityInjection(@Nullable Integer nullableBean, String nonNullBean) {
if(nullableBean != null){ this.nullableBean = nullableBean;
this.nullableBean = nullableBean;
}
this.nonNullBean = nonNullBean; this.nonNullBean = nonNullBean;
} }
} }
static class MixedOptionalInjectionBean{
public NonNullBean nonNullBean; static class MixedOptionalInjectionBean {
public NullableBean nullableBean;
@Nullable
public Integer nullableBean;
public String nonNullBean;
@Autowired(required = false) @Autowired(required = false)
public void optionalInjection(Optional<NullableBean> optionalBean, NonNullBean nonNullBean){ public void optionalInjection(Optional<Integer> optionalBean, String nonNullBean) {
optionalBean.ifPresent(bean -> this.nullableBean = bean); optionalBean.ifPresent(bean -> this.nullableBean = bean);
this.nonNullBean = nonNullBean; this.nonNullBean = nonNullBean;
} }