When @Autowired(require=false)
on mixed nullability args method, I think IOC container shoud call this method to inject partially non-null parameters, but in fact the current code logic will ignore this method injection, for example :
static class MixedNullableInjectionBean{
public NonNullBean nonNullBean;
public NullableBean nullableBean;
@Autowired(required = false)
public void nullabilityInjection(@Nullable NullableBean nullableBean, NonNullBean nonNullBean){
if(nullableBean != null){
this.nullableBean = nullableBean;
}
this.nonNullBean = nonNullBean;
}
}
@Test
public void testMethodInjectionWithMultiMixedNullableArgs(){
bf.registerBeanDefinition("nonNullBean", new RootBeanDefinition(
NonNullBean.class));
bf.registerBeanDefinition("mixedNullableInjectionBean", new RootBeanDefinition(MixedNullableInjectionBean.class));
MixedNullableInjectionBean mixedNullableInjectionBean = bf.getBean(MixedNullableInjectionBean.class);
assertThat(mixedNullableInjectionBean.nonNullBean).isNotNull();
assertThat(mixedNullableInjectionBean.nullableBean).isNull();
}
I liberated the @Autowired(require=false)
annotation on the nullabilityInjection
method of MixedNullableInjectionBean
, this method has two parameters, nullableBean
and nonNullBean
, as the name of the parameter means, I only register nonNullBean
to DefaultListableBeanFactory
, and then register MixedNullableInjectionBean
.
I think the IOC container should invoke nullabilityInjection
method to inject nonNullBean
,
but in fact the current code logic will ignore this method injection, I have expressed the nullability of the parameter nullableBean
, so, I think this parameter is null should not affect the overall method injection. I slightly adjusted the logic of method injection in this pr, what do you think? Thank you for your reading and looking forward to your reply.
Comment From: snicoll
Good stuff, thanks again @chenqimiao
This was closed with the wrong issue reference: https://github.com/spring-projects/spring-framework/commit/d0fc6dd06d69d9095d909a02c395f456139f792d