Improve @Autowired method injection on mixed nullability args
See gh-17215
This commit is contained in:
		
							parent
							
								
									c8169e5cad
								
							
						
					
					
						commit
						32c0540424
					
				|  | @ -865,7 +865,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA | |||
| 				descriptors[i] = currDesc; | ||||
| 				try { | ||||
| 					Object arg = beanFactory.resolveDependency(currDesc, beanName, autowiredBeanNames, typeConverter); | ||||
| 					if (arg == null && !this.required) { | ||||
| 					if (arg == null && !this.required && !methodParam.isOptional()) { | ||||
| 						arguments = null; | ||||
| 						break; | ||||
| 					} | ||||
|  |  | |||
|  | @ -32,6 +32,7 @@ import java.util.LinkedHashMap; | |||
| import java.util.LinkedHashSet; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Optional; | ||||
| import java.util.Properties; | ||||
| import java.util.Set; | ||||
| import java.util.SortedMap; | ||||
|  | @ -2600,6 +2601,26 @@ public class AutowiredAnnotationBeanPostProcessorTests { | |||
| 		assertThat(bean.testBean).isSameAs(bf.getBean("annotatedBean")); | ||||
| 	} | ||||
| 
 | ||||
| 	@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(); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void testMethodInjectionWithMultiMixedOptionalArgs(){ | ||||
| 		bf.registerBeanDefinition("nonNullBean", new RootBeanDefinition( | ||||
| 				NonNullBean.class)); | ||||
| 		bf.registerBeanDefinition("mixedOptionalInjectionBean", new RootBeanDefinition(MixedOptionalInjectionBean.class)); | ||||
| 		MixedOptionalInjectionBean mixedOptionalInjectionBean = bf.getBean(MixedOptionalInjectionBean.class); | ||||
| 		assertThat(mixedOptionalInjectionBean.nonNullBean).isNotNull(); | ||||
| 		assertThat(mixedOptionalInjectionBean.nullableBean).isNull(); | ||||
| 	} | ||||
| 
 | ||||
| 	private <E extends UnsatisfiedDependencyException> Consumer<E> methodParameterDeclaredOn( | ||||
| 			Class<?> expected) { | ||||
| 		return declaredOn( | ||||
|  | @ -4346,4 +4367,35 @@ public class AutowiredAnnotationBeanPostProcessorTests { | |||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	static class NullableBean { | ||||
| 
 | ||||
| 	} | ||||
| 	static class NonNullBean { | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	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; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	static class MixedOptionalInjectionBean{ | ||||
| 		public NonNullBean nonNullBean; | ||||
| 		public NullableBean nullableBean; | ||||
| 
 | ||||
| 		@Autowired(required = false) | ||||
| 		public void optionalInjection(Optional<NullableBean> optionalBean, NonNullBean nonNullBean){ | ||||
| 			optionalBean.ifPresent(bean -> this.nullableBean = bean); | ||||
| 			this.nonNullBean = nonNullBean; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue