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

  • configuring via factory methods

    Hi,

    I have several beans whose implementation class is different for each customer. For the sake of simplicity assume there are just 2 beans (paint and carpet) and 2 customers (red and blue)

    Code:
    <!-- 
    Include one or other of the 2 sets of beans below, depending on
    whether we are building for the red or blue customer
    -->
    <bean id="paint" class="com.paint.RedPaint" />
    <bean id="carpet" class="com.carpet.RedCarpet" />
    
    <bean id="paint" class="com.paint.BluePaint" />
    <bean id="carpet" class="com.carpet.BlueCarpet" />
    Manually including/excluding the correct beans is annoying, particularly when there are many customers/beans.
    What I would like to do is define an abstract factory interface like this:

    Code:
    interface CustomerFactory {
        Paint getPaint();
        Carpet getCarpet();
    }
    And a bean in the Spring config that provides a customer-specific implementation of this factory:

    Code:
    <bean id="customerFactory" class="com.customer.BlueFactory" />
    Now if I want to build the application for the red customer I only need to change one thing

    Code:
    <bean id="customerFactory" class="com.customer.RedFactory" />
    However, there's one thing I don't know how to do......how to create the "paint" and "carpet" beans by calling a method of the "customerFactory" bean?

    In the Spring docs an example is given of how to create a bean by calling a factory method of a class, but that's not what I need here, I want a way to create a bean by calling a method of a bean.

    Regards,
    DM

  • #2
    Why not put all customer specific beans in the configuration file instead of hacking around with a factory?

    However if you really want to go that way, you can use a MethodInvokingFactoryBean which calls a method on your specified target.

    Another solution (assuming the properties/constructors are the same) is to specify the needed class in a property file and use a PropertyPlaceHolderConfigurer to replace the classes. That way you can keep the configuration out of your build.

    properties file for red
    Code:
    paint.class=com.paint.RedPaint
    carpet.class=com.carpet.RedCarpet
    Bean configuration
    Code:
    <bean id="carpet" class="${carpet.class}"/>
    
    <bean class="PropertyPlaceHolderConfigurer">
      <property name="location" value="file:/path/to/propertyfile.properties"/>
    </bean>
    So the only thing you need to do is specify a environment specific properties file and deploy the application, no need for a custom build or anything. Just drop your default application and the custom war/ear in place and you should be good to go.
    Last edited by Marten Deinum; Jul 13th, 2007, 04:34 AM.

    Comment


    • #3
      Originally posted by domurtag View Post
      I want a way to create a bean by calling a method of a bean
      Is this what you are looking for?

      http://static.springframework.org/sp...factory-method

      Cheers
      Rick

      Comment


      • #4
        Originally posted by mdeinum View Post
        Why not put all customer specific beans in the configuration file instead of hacking around with a factory?
        I can't put all the customer-specific beans in the config file, because I would then have multiple beans with the same name

        Originally posted by mdeinum View Post
        However if you really want to go that way, you can use a MethodInvokingFactoryBean which calls a method on your specified target.
        I had a look at the documentation for this class, but it doesn't show an example of how to use it to set the class of another bean in the Spring config. Perhaps it's only intended to be used by the application code?

        Originally posted by mdeinum View Post
        Another solution (assuming the properties/constructors are the same) is to specify the needed class in a property file and use a PropertyPlaceHolderConfigurer to replace the classes. That way you can keep the configuration out of your build.
        That's a good solution, but I'm still curious to know if it's possible to create a bean by calling a method of another bean

        Comment


        • #5
          Originally posted by Rick Evans View Post
          That is exactly what I was looking for

          Comment

          Working...
          X