From javadoc of org.springframework.beans.factory.support.ConstructorResolver#autowireConstructor
:
"autowire constructor" (with constructor arguments by type) behavior. Also applied if explicit constructor argument values are specified, matching all remaining arguments with beans from the bean factory.
So having that in mind I would expect that constructor injection would work with some of arguments injected and some provided, but it fails - reproduction code
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AssistedInjectionFails.B.class, AssistedInjectionFails.C.class})
public class AssistedInjectionFails {
public static class A {
}
@Component
public static class B {
}
@Component
@Scope("prototype")
public static class C {
private final A a;
private final B b;
@Autowired
public C(A a, B b) {
this.a = a;
this.b = b;
}
}
@Autowired
private ApplicationContext context;
@Test
public void test() {
C c = context.getBean(C.class, new A());
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assistedInjectionFails.C': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Comment From: jhoeller
That javadoc is out of sync with the algorithm - but it's only really on an internal non-public class, so not part of any user contract.
That said, we should clarify what the expectations for a getBean
call with provided arguments are in the BeanFactory
javadoc itself. I'll repurpose this ticket for it.