Announcement Announcement Module
Collapse
No announcement yet.
Configuring bean lists from properties? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring bean lists from properties?

    Hi,

    I have a bean definition basically like this:

    Code:
    <bean id="factory">
      <property name="listeners">
        <map>
          <entry type="type1">
            <list>
              <bean class="listener.type.one" />
              <bean class="listener.type.two" />
            </list>
          </entry>
        </map>
      </property>
    </bean>
    I use this bean definition as part of a module that gets plugged into
    a number of larger applications. In one of these apps, I have to redo
    this definition because I have a third listener.

    What I want do is, to define the listeners in a properties file, e.g.

    Code:
    factory.listeners.type1 = listener.type.one, listener.type.two
    and for my special case:

    Code:
    factory.listeners.type1 = listener.type.one, listener.type.two, listener.type.three
    and use some sort of placeholder replacement for the

    Code:
    <list>
      <bean class="listener.type.one" />
      <bean class="listener.type.two" />
    </list>
    block of the bean definition. Or just the list itself. I scratched my
    head over BeanFactoryPostProcessor definitions but I simply can not
    see a way to intercept the List creation at bean creation time or the
    variable number of elements at declaration time. I'm very grateful for
    hints.

    Thanks
    Henning

  • #2
    Have you looked at Collection merging at all?
    http://www.springframework.org/docs/...ction-elements
    Last edited by karldmoore; Aug 27th, 2007, 04:40 PM.

    Comment


    • #3
      Yes, of course. If I use collection merging, then I would need a child bean definition in every application, something that I try to avoid because then the module (which also uses the factory) would need a dependency on the child bean. Something like this:

      Current state:

      module:

      Code:
      <beans>
        <bean id="factory">
         ...
        </bean>
        <bean id="factoryUser">
          <property name="factory" ref="factory"/>
          ...
        </bean>
      </beans>
      application:

      Code:
      <beans>
        <bean id="appFactoryUser">
          <property name="factory" ref="factory"/>
          ...
        </bean>
      </beans>

      When using Collection merging:

      module:

      Code:
      <beans>
        <bean id="abstractFactory" abstract="true">
         ...
        </bean>
        <bean id="factoryUser">
          <property name="factory" ref="factory"/> [1]
          ...
        </bean>
      </beans>
      application:

      Code:
      <beans>
        <bean id="factory" parent="abstractFactory">
         [... do our happy collection merging ...]
        </bean>
      
        <bean id="appFactoryUser">
          <property name="factory" ref="factory"/>
          ...
        </bean>
      </beans>
      Now you have a back-reference between module and application at [1]: The module expects the application to define a bean called "factory". Bad. I also have an abstract Bean that I did not need before. Makes the module harder to test.

      Yes, I did read the manual. I even understood most of it. :-)

      Comment


      • #4
        I'm sure I've seen examples in the past using PropertyPlaceholderConfigurer and relying on StringArrayPropertyEditor convert the injected value.
        http://www.springframework.org/docs/...rtyEditor.html
        Last edited by karldmoore; Aug 27th, 2007, 04:40 PM.

        Comment


        • #5
          I have a list of bean objects, not a list of bean names. And I need this as
          value part of a map, which takes object, so a PropertyEditor will probably not
          work here.

          Comment

          Working...
          X