Results 1 to 7 of 7

Thread: Bean creation via static factory method

  1. #1
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    10

    Default Bean creation via static factory method

    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:
    Code:
    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

  2. #2
    Join Date
    Aug 2004
    Location
    St. Louis, MO, USA
    Posts
    24

    Default Re: Bean creation via static factory method

    Something like this will work:

    Code:
    <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.

  3. #3
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  4. #4
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    10

    Default

    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

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Andreas,

    Since
    They do have a static getOfferInstance() but not a static TheirsRemoteObject getTheirsRemoteObject()????
    then you should use
    Code:
      <bean id="myController" class="com.foo.SomeController"> 
        <property name="remoteObj"> 
          <bean factory-method="getOfferInstance" class="com.foo.RemoteFactory" /> 
        </property> 
      </bean>
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  6. #6

    Default

    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

  7. #7
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    10

    Default

    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

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •