Results 1 to 5 of 5

Thread: Bean creation via an instance factory method

  1. #1
    Join Date
    Jul 2006
    Location
    Kolkata, India
    Posts
    217

    Default Bean creation via an instance factory method

    Hi -

    When I am creating a bean via instance factory method, can I specify in the xml an argument for "factory-method" ? e.g. in the following snippet, can I specify that my factory method createInstance() takes an argument which determines the exact type of bean instantiated.

    <!-- The factory bean, which contains a method called <literal>createInstance()</literal> -->
    <bean id="myFactoryBean" class="...">
    ...
    </bean>
    <!-- The bean to be created via the factory bean -->
    <bean id="exampleBean"
    factory-bean="myFactoryBean"
    factory-method="createInstance"/>

    This is useful if the factory creates a polymorphic type, where the actual type instantiated will depend on this argument.

    Please help.

    - Debasish

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    From the spring-beans.dtd:

    The factory method can have any number of arguments. Autowiring is not
    supported. Use indexed constructor-arg elements in conjunction with the
    factory-method attribute.
    Hope that helps,
    Andreas

  3. #3
    Join Date
    Jul 2006
    Location
    Kolkata, India
    Posts
    217

    Default

    Thanks a lot .. I should have looked at the example in the Spring reference doc.

  4. #4
    Join Date
    Apr 2006
    Posts
    9

    Question

    Quote Originally Posted by debasishg View Post
    Hi -

    <!-- The factory bean, which contains a method called <literal>createInstance()</literal> -->
    <bean id="myFactoryBean" class="...">
    ...
    </bean>
    <!-- The bean to be created via the factory bean -->
    <bean id="exampleBean"
    factory-bean="myFactoryBean"
    factory-method="createInstance"/>
    myFactoryBean should create differente beans based on some values passed to createInstance() but these values are known only at runtime; At config time I know only the type (String, int ...) of the parameters of createInstance(). Is it possible to do this? how?

    Thanks in advance

  5. #5
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Maybe have a look here. Code is also available.

    Regards,
    Andreas

Posting Permissions

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