You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ObjectFactory lacks method for getting bean with specified constructor arguments [SPR-13956] #18529
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.
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.
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.
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.
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?
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.
Activity
spring-projects-issues commentedon Feb 17, 2016
Juergen Hoeller commented
This looks like a straightforward addition to 4.3's new
SmartObjectFactory, as introduced in #18515...spring-projects-issues commentedon Feb 17, 2016
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.
spring-projects-issues commentedon Feb 17, 2016
Juergen Hoeller commented
Well,
SmartObjectFactoryis not strictly about optional availability. It is a way to introduce new methods withour breaking custom implementors ofObjectFactoryitself: 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 existinggetObject()signature in a non-binary-compatible way there.From another perspective, conceptually,
ObjectFactoryis 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 behindObjectFactoryitself to a different level.SmartObjectFactoryalso serves as a way of separating those purposes.Juergen
spring-projects-issues commentedon Feb 17, 2016
Juergen Hoeller commented
Actually, the problem is even worse:
@Autowired ObjectFactoryhandles share the exact same resolution rules as regular@Autowiredinjection 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.spring-projects-issues commentedon Feb 18, 2016
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?
spring-projects-issues commentedon Feb 18, 2016
Juergen Hoeller commented
It's just a problem with the current implementation arrangement.
DefaultListableBeanFactory.resolveDependencyand theDependencyDescriptormechanism, as used behind@Autowiredresolution, 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
spring-projects-issues commentedon Feb 19, 2016
Juergen Hoeller commented
Committed to master as
getObject(Object... args)onSmartObjectFactory. Feel free to give it a try in an upcoming4.3.0.BUILD-SNAPSHOT...Juergen
spring-projects-issues commentedon Feb 23, 2016
Juergen Hoeller commented
Quick note:
SmartObjectFactoryhas been renamed toObjectProvider(extendsObjectFactory).