View Full Version : Several beans with the same descriptors
Aug 20th, 2004, 05:15 AM
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)?
Aug 20th, 2004, 05:57 AM
Have you considered the "parent" attribute in the XML definition? This enables you to "inherit" the common wiring.
Aug 20th, 2004, 08:46 AM
Here's a description of using child beans:
Aug 20th, 2004, 04:59 PM
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)
Aug 20th, 2004, 05:15 PM
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.
Aug 20th, 2004, 05:44 PM
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?
Aug 20th, 2004, 05:47 PM
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.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.