Results 1 to 7 of 7

Thread: Several beans with the same descriptors

Hybrid View

  1. #1
    Join Date
    Aug 2004
    Posts
    21

    Default 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)?

    /Marcus

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

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

  3. #3
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Here's a description of using child beans:

    http://www.springframework.org/docs/...an-definitions
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  4. #4
    Join Date
    Aug 2004
    Posts
    21

    Default

    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>

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


    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. #5
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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.

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  6. #6
    Join Date
    Aug 2004
    Posts
    21

    Default

    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?

    /Marcus

  7. #7
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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.

Similar Threads

  1. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  2. Replies: 0
    Last Post: Aug 25th, 2005, 05:11 AM
  3. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM
  4. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  5. using commons-beanutils on Spring beans
    By boz in forum Container
    Replies: 6
    Last Post: Sep 25th, 2004, 12:46 AM

Posting Permissions

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