Announcement Announcement Module
Collapse
No announcement yet.
Instantiate Bean by external Factory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Instantiate Bean by external Factory

    Hello,

    I want a Spring Bean to be created by a Factory method of an included (non spring) library and I don't know how to proceed.

    The included class is:
    Code:
    com.abc.model.ModelFactory
    and it has a method called
    Code:
    ModelFactory.createModel(String uri)
    which creates a model from a given uri. Now I want to tell Spring to use a certain uri and create a bean representing such a model.

    Obviously this doesn't work:
    Code:
    <bean id="myModel" class="com.abc.model.ModelFactory" init-method="??">
      <how to set the String parameter?>
    </bean>
    Anyone an idea?

  • #2
    From Spring reference, chapter "3.2.3.2.2. Instantiation using a static factory method" and "3.3.1.3 Some Examples"

    Code:
    Now consider a variant of this where instead of using a constructor, Spring is told to call a static factory
    method to return an instance of the object:
    <bean id="exampleBean" class="examples.ExampleBean"
    factory-method="createInstance">
    <constructor-arg ref="anotherExampleBean"/>
    <constructor-arg ref="yetAnotherBean"/>
    <constructor-arg value="1"/>
    </bean>
    <consructor-arg>s are used to pass parameters to factory method.

    Regards,
    Oleksandr



    Originally posted by TSH View Post
    Hello,

    I want a Spring Bean to be created by a Factory method of an included (non spring) library and I don't know how to proceed.

    The included class is:
    Code:
    com.abc.model.ModelFactory
    and it has a method called
    Code:
    ModelFactory.createModel(String uri)
    which creates a model from a given uri. Now I want to tell Spring to use a certain uri and create a bean representing such a model.

    Obviously this doesn't work:
    Code:
    <bean id="myModel" class="com.abc.model.ModelFactory" init-method="??">
      <how to set the String parameter?>
    </bean>
    Anyone an idea?

    Comment

    Working...
    X