View Full Version : Bean creation via static factory method
andreas
Aug 13th, 2004, 07:54 AM
Hi,
I have read the reference document and found a method to create beans using a static factory method but unfortunately I can't seem to get it to work.
I have a class called RemoteFactory that I must use to create an instance of a object. This Remotefactory has a number of different getInstance method that retrieves different objects dependent on which getxxxInstance that is called. I would like to wire this bean in to one of my controllers by calling one of those getxxInstance method that returns an instance of TheirsRemoteObject.
This is how I would do it manually:
TheirsRemoteObject obj = RemoteFactory.getTheirsRemoteObjectInstance();
MoreVO[] moreVo = obj.callMethod();
So I would like to have this TheirsRemoteObject as a bean property on my controller. Also I do know which object I expect after asking the factory about it.
I would really appreciate some advice on if this is possible and if so how I should do it.
Thanks
Andreas
james.estes
Aug 13th, 2004, 01:52 PM
Something like this will work:
<bean id="myController" class="com.foo.SomeController">
<property name="theirsRemoteObject">
<bean factory-method="getTheirsRemoteObjectInstance" class="com.foo.RemoteFactory" />
</property>
</bean>
Then, com.foo.SomeController will have a setTheirsRemoteObject(TheirsRemoteObject obj) method, and com.foo.RemoteFactory will have a static TheirsRemoteObject getTheirsRemoteObject() method. (Theirs remote object should probably be an interface, though this is not a requirement).
I almost didn't think it would work because the SpringIDE plugin complains about the inner bean: "factory-method" must be declared for element type "bean". But it works at runtime.
Colin Sampaleanu
Aug 13th, 2004, 02:17 PM
James' code should work fine I think, although there's no requirement to do it as an inner bean. It could of course be a separate bean which has a factory-method defined.
andreas
Aug 13th, 2004, 04:28 PM
Hi,
thanks a lot for the answers. Unfortunately I still can't get it to work. I get Attribute "factory-method" is not declared for element "bean" at both deploy and runtime.
Their RemoteFactory returns me an instance of an Offer object.
I have setters on my controller and I have been able to get a instance from their RemoteFactory.
"Then, com.foo.SomeController will have a setTheirsRemoteObject(TheirsRemoteObject obj) method, and com.foo.RemoteFactory will have a static TheirsRemoteObject getTheirsRemoteObject() method"
They (as in it is not my code but a client API has been given to me). They do have a static getOfferInstance() but not a static TheirsRemoteObject getTheirsRemoteObject()????
When I ask to get an instance of the Offer object programmatically it works fine;
Offer offer = RemoteBusinessFactory.getOfferInstance();
On my controllers I have getRemoteObj and setRemoteObj(Offer offer) and I do as you guys recommended but still no success.
Any other ideas? Are there any restrictions on they interface for the RemoteFactory?
Anyway it is not a big issue for me since it is easy to get the instance anyway but it would be nice to get it to work.
Cheers
Andreas
irbouho
Aug 13th, 2004, 05:01 PM
Andreas,
Since
They do have a static getOfferInstance() but not a static TheirsRemoteObject getTheirsRemoteObject()????
then you should use
<bean id="myController" class="com.foo.SomeController">
<property name="remoteObj">
<bean factory-method="getOfferInstance" class="com.foo.RemoteFactory" />
</property>
</bean>
gpoirier
Aug 13th, 2004, 05:18 PM
I get Attribute "factory-method" is not declared for element "bean" at both deploy and runtime.
Which version of Spring are you using? The factory-method has been introduced in 1.1 RC1, it was not in 1.0.2.
Guillaume
andreas
Aug 13th, 2004, 05:30 PM
Guillaume,
THANKS A LOT.
You just saved me many hours since I was starting to get really frustrated since my first try was exaclty as has been proposed by the other guys.
Thank you all for taking time on this.
Cheers
Andreas
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.