Skip to content

Commit d0fc6dd

Browse files
committed
Merge pull request #17215 from chenqimiao
* pr/24773: Polish "Improve @Autowired method injection on mixed nullability args" Improve @Autowired method injection on mixed nullability args Closes gh-17215
2 parents c8169e5 + 8efc7a9 commit d0fc6dd

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ private Object[] resolveMethodArguments(Method method, Object bean, @Nullable St
865865
descriptors[i] = currDesc;
866866
try {
867867
Object arg = beanFactory.resolveDependency(currDesc, beanName, autowiredBeanNames, typeConverter);
868-
if (arg == null && !this.required) {
868+
if (arg == null && !this.required && !methodParam.isOptional()) {
869869
arguments = null;
870870
break;
871871
}

Diff for: spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

+51
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.LinkedHashSet;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.Optional;
3536
import java.util.Properties;
3637
import java.util.Set;
3738
import java.util.SortedMap;
@@ -2600,6 +2601,26 @@ void factoryBeanSelfInjectionViaFactoryMethod() {
26002601
assertThat(bean.testBean).isSameAs(bf.getBean("annotatedBean"));
26012602
}
26022603

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+
26032624
private <E extends UnsatisfiedDependencyException> Consumer<E> methodParameterDeclaredOn(
26042625
Class<?> expected) {
26052626
return declaredOn(
@@ -4346,4 +4367,34 @@ public static TestBean newTestBean2() {
43464367
}
43474368
}
43484369

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+
43494400
}

0 commit comments

Comments
 (0)