Announcement Announcement Module
No announcement yet.
Several beans with the same descriptors Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Several beans with the same descriptors

    I have several controller beans that has identical wiring (they do infact inherit the same abstract class that extends AbstractController). It's a bit tedious to add this wiring to each and every bean.

    Is there a way to "auto-wire" based on implemented interfaces on the bean (ie. I specify a mapping for a certain interface and all beans implementing that interface will be wired according to the descriptor of that interface)?


  • #2
    Have you considered the "parent" attribute in the XML definition? This enables you to "inherit" the common wiring.


    • #3
      Here's a description of using child beans:


      • #4
        I've tried the following:

        <bean id="abstractController" lazy-init="true" class="se.dreampark.controllers.Subscribe1">
        <property name="urlHelper"><ref local="urlHelper"/></property>
        <property name="aniFactory"><ref bean="aniServiceFactoryMock"/></property>
        <property name="labels"><ref bean="labels"/></property>

        <bean id="portalController" parent="abstractController" class="se.dreampark.controllers.PortalController">
        <property name="portalFactory"><ref bean="portalFactory"/></property>

        where abstractController just points to a concrete implementation of my abstract controller. Then I set parent to abstractController in my portalController with the hope that it would get populated with the properties defined in abstractController.

        Shouldn't all properties declared in abstractController be set on portalController now? It surely doesn't. I get a null pointer exception in portalController when I try to use the urlHelper. It does work when I explicitly declare the properties in the portalController definition (but that is exactly what I didn't want to do).

        What do I do wrong?

        Btw, lazy-init was taken from the docs. I don't know if it's needed (the result is the same with or without it)


        • #5
          First of all, if only the child is going to be instantiated, so the parent can be considered an abstract def, then you only need to specify the class in one place, either the parent if all children will use the same class, or in each child if each child has a different class.

          As to why the props aren't being set, are you running Spring 1.1RC1 or later? The full parent/child bean def capabilities didn't exist before then; there was only a limited form.



          • #6
            I want to consider the abstractController an abstract definition (never to be used except as a template). Each child bean will have it's own class.

            But I can't remove the class attribute from abstractController since the XML validator will complain about me not following the DTD. Maybe I could set parent to something but I don't know what to set it to when it doesn't have a parent.

            The version I use is currently 1.0.2 so maybe I should switch?



            • #7
              You need to use 1.1RC1 or later. Like I just said, there was a weaker form of parent and child bean capability pervious to that, but to tell you the truth, I don't remember exactly how much it could do, as it wasn't really documented well. The current capability is fully documented in the manual, and works.