Skip to content

ObjectFactory lacks method for getting bean with specified constructor arguments [SPR-13956] #18529

Closed

Description

@spring-projects-issues

Frank Scheffler opened SPR-13956 and commented

While BeanFactory supports retrieving prototyped beans with additional constructor or factory-method arguments (#15860), the org.springframework.beans.factory.ObjectFactory is still missing such method.

ObjectFactory is often used in test fixtures, where lookup-method injection et.al. cannot be used, in order to get access to prototype bean instance. Using the BeanFactory directly in such cases is currently the only way to solve this.


Affects: 4.2.4

Issue Links:

Referenced from: commits ed98393, 890819f

Activity

  1. spring-projects-issues commented on Feb 17, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This looks like a straightforward addition to 4.3's new SmartObjectFactory, as introduced in #18515...

  2. spring-projects-issues commented on Feb 17, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Frank Scheffler commented

    I am not sure SmartObjectFactory is the right place, since it is not about whether the bean is available or not. Actually, I was thinking about the equivalent of BeanFactory.getObject(Class<T>, Object... parameters), simply without providing the type, since ObjectFactory already knows the type.

    One could even go as far as adding the var-args parameters to the existing getObject()-method, however the JavaDoc may become a little confusing in that case.

  3. spring-projects-issues commented on Feb 17, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Well, SmartObjectFactory is not strictly about optional availability. It is a way to introduce new methods withour breaking custom implementors of ObjectFactory itself: That interface serves for several purposes, not just as an injection point handle, and we can't easily add methods to it or change the existing getObject() signature in a non-binary-compatible way there.

    From another perspective, conceptually, ObjectFactory is just a plain retrieval mechanism which implies no bean container concepts. Optionality, uniqueness and also the notion of specific parameters for prototype beans are all container-related and would therefore take the conceptual burden behind ObjectFactory itself to a different level. SmartObjectFactory also serves as a way of separating those purposes.

    Juergen

  4. spring-projects-issues commented on Feb 17, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Actually, the problem is even worse: @Autowired ObjectFactory handles share the exact same resolution rules as regular @Autowired injection points. That algorithm (and the corresponding SPI) only works towards regularly created bean instances at the moment, with no existing way to pass in construction arguments. We'll see what we can do to open this up.

  5. spring-projects-issues commented on Feb 18, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Frank Scheffler commented

    Could you explain that a little more? I don't see, why the injection of ObjectFactory itself is a problem. As far as I know, ObjectFactory can be injected for both singleton and prototype beans. How is that going to be affected by changing the method signature (or adding a method) to the injected factory itself?

  6. spring-projects-issues commented on Feb 18, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    It's just a problem with the current implementation arrangement. DefaultListableBeanFactory.resolveDependency and the DependencyDescriptor mechanism, as used behind @Autowired resolution, do not allow for passing in arguments at this point. That algorithm has been revised for 4.3 already, so it makes sense to tackle your arguments case as well. I'll give it a try ASAP.

    Juergen

  7. spring-projects-issues commented on Feb 19, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Committed to master as getObject(Object... args) on SmartObjectFactory. Feel free to give it a try in an upcoming 4.3.0.BUILD-SNAPSHOT...

    Juergen

  8. spring-projects-issues commented on Feb 23, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Quick note: SmartObjectFactory has been renamed to ObjectProvider (extends ObjectFactory).

  9. added
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions