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

Clarify assisted constructor injection through getBean call with provided arguments #24955

Closed
Antoniossss opened this issue Apr 22, 2020 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@Antoniossss
Copy link

Antoniossss commented Apr 22, 2020

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)
@Antoniossss Antoniossss changed the title Assised constructor injection is not working - in contrary to what javadoc says Assissted constructor injection is not working - in contrary to what javadoc says Apr 22, 2020
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 22, 2020
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Apr 22, 2020
@snicoll snicoll self-assigned this Nov 2, 2023
@jhoeller jhoeller assigned jhoeller and unassigned snicoll Dec 21, 2023
@jhoeller jhoeller added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 21, 2023
@jhoeller jhoeller added this to the 6.1.3 milestone Dec 21, 2023
@jhoeller jhoeller changed the title Assissted constructor injection is not working - in contrary to what javadoc says Clarify assisted constructor injection through getBean with provided arguments Dec 21, 2023
@jhoeller
Copy link
Contributor

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.

@jhoeller jhoeller changed the title Clarify assisted constructor injection through getBean with provided arguments Clarify assisted constructor injection through getBean call with provided arguments Dec 21, 2023
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: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

5 participants