Skip to content

Commit

Permalink
Merge pull request #17215 from chenqimiao
Browse files Browse the repository at this point in the history
* pr/24773:
  Polish "Improve @Autowired method injection on mixed nullability args"
  Improve @Autowired method injection on mixed nullability args

Closes gh-17215
  • Loading branch information
snicoll committed Aug 25, 2023
2 parents c8169e5 + 8efc7a9 commit d0fc6dd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ private Object[] resolveMethodArguments(Method method, Object bean, @Nullable St
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -2600,6 +2601,26 @@ void factoryBeanSelfInjectionViaFactoryMethod() {
assertThat(bean.testBean).isSameAs(bf.getBean("annotatedBean"));
}

@Test
public void mixedNullableArgMethodInjection(){
bf.registerSingleton("nonNullBean", "Test");
bf.registerBeanDefinition("mixedNullableInjectionBean",
new RootBeanDefinition(MixedNullableInjectionBean.class));
MixedNullableInjectionBean mixedNullableInjectionBean = bf.getBean(MixedNullableInjectionBean.class);
assertThat(mixedNullableInjectionBean.nonNullBean).isNotNull();
assertThat(mixedNullableInjectionBean.nullableBean).isNull();
}

@Test
public void mixedOptionalArgMethodInjection(){
bf.registerSingleton("nonNullBean", "Test");
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(
Expand Down Expand Up @@ -4346,4 +4367,34 @@ public static TestBean newTestBean2() {
}
}


static class MixedNullableInjectionBean {

@Nullable
public Integer nullableBean;

public String nonNullBean;

@Autowired(required = false)
public void nullabilityInjection(@Nullable Integer nullableBean, String nonNullBean) {
this.nullableBean = nullableBean;
this.nonNullBean = nonNullBean;
}
}


static class MixedOptionalInjectionBean {

@Nullable
public Integer nullableBean;

public String nonNullBean;

@Autowired(required = false)
public void optionalInjection(Optional<Integer> optionalBean, String nonNullBean) {
optionalBean.ifPresent(bean -> this.nullableBean = bean);
this.nonNullBean = nonNullBean;
}
}

}

0 comments on commit d0fc6dd

Please sign in to comment.