Announcement Announcement Module
Collapse
No announcement yet.
Bean creation via static factory method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment


        • #5
          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>

          Comment


          • #6
            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

            Comment


            • #7
              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

              Comment

              Working...
              X