Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support method injection when @Autowired(require=false) is used on mixed nullability args #24773

Conversation

chenqimiao
Copy link
Contributor

@chenqimiao chenqimiao commented Mar 25, 2020

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 25, 2020
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 25, 2023
@jhoeller jhoeller added this to the 6.1.0-RC1 milestone Aug 25, 2023
@snicoll snicoll self-assigned this Aug 25, 2023
@snicoll snicoll changed the title Improve method injection when @Autowired(require=false) on mixed nullability args method Support method injection when @Autowired(require=false) is used on mixed nullability args Aug 25, 2023
@snicoll
Copy link
Member

snicoll commented Aug 25, 2023

Good stuff, thanks again @chenqimiao

This was closed with the wrong issue reference: d0fc6dd

@snicoll snicoll closed this Aug 25, 2023
@chenqimiao chenqimiao deleted the mixed-nullability-method-injection branch August 28, 2023 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants