Results 1 to 5 of 5

Thread: Configuring bean lists from properties?

  1. #1
    Join Date
    Jul 2007
    Posts
    5

    Default 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. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Have you looked at Collection merging at all?
    http://www.springframework.org/docs/...ction-elements
    Last edited by karldmoore; Aug 27th, 2007 at 03:40 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3
    Join Date
    Jul 2007
    Posts
    5

    Default

    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. :-)

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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 at 03:40 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #5
    Join Date
    Jul 2007
    Posts
    5

    Default

    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.

Posting Permissions

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