Announcement Announcement Module
Collapse
No announcement yet.
Bean inheritance - why is it done only on the xml? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean inheritance - why is it done only on the xml?

    Hi,

    I haven't trolled throught the forums about this topic (oh, fine, it takes all of a minute, I'll do that... NOW)

    Ok, so I just had a quick glance and can't see this anywhere

    When I use inheritance on my cool spring beans, as in, declare one as abstract and the other as a child of the abstract, the abstract bean isn't actually instantiated in any way.
    Which is obvious.
    But, let's say I have the following scenario:
    Code:
    	<bean id="parent" abstract="true">
                    //a plethora of properties
    		<property name="parameterValues">
    	      	<map>
    	      		<entry key="key1">
    	      			<value>key1</value>
    	      		</entry>
    	      	</map>
          </property>
    
    
         	<bean id="child" parent="parent" class="my.domain.here.DataRequest">
                   // more properties
    		<property name="parameterValues">
      			<map>
      				<entry key="key2">
      					<value>key2</value>
      				</entry>
      			</map>
    		</property>
    	</bean>
    The problem I'm finding is that the map defined in the abstract bean is completely thrown away by the child bean, and only the child bean's (in this case, child's) parameterValues map is preserved.

    I actually went to some lengths in the DataRequest class to ensure I could actually do the above:

    Code:
    public void setParameterValues(Map<String, String> defaultParameters) {
            if (this.parameterValues == null)
                this.parameterValues = defaultParameters;
            else {
                if (defaultParameters == null)
                    this.parameterValues = null;
                else {
                    this.parameterValues.putAll(defaultParameters);
                }
            }
        }
    This lets me keep on adding stuff to the parameterValues.

    So, to recap:
    How I thought spring worked - I declare an abstract bean, it instantiates/does stuff in some way, and then I declare a child bean.
    That child bean has all the properties of the parent AND the child.

    How it looks like spring works - I declare an abstract bean, it does nothing. It's just xml. Then I declare a child bean, that child has all the non-conflicting -property-name values of the parent AND the child. If a property name conflict occurs, only the child bean's is used.

    Is this so? And if so, why?

    Cheers,
    Andrew
    Last edited by bharal; Jul 23rd, 2007, 04:42 PM.

  • #2
    Hi Andrew,
    I'm fairly new to spring but I would think that you may still use the parent-child relationship but you'd have to instantiate it in the child like :

    Code:
    <bean id="parent" abstract="true">
                    //a plethora of properties
    		<property name="parameterValues">
    	            </property>
    
    
         	<bean id="child" parent="parent" class="my.domain.here.DataRequest">
                   // more properties
    		<property name="parameterValues">
      			<map>
    
      				<entry key="key1">
      					<value>key1</value>
      				</entry>
    
      				<entry key="key2">
      					<value>key2</value>
      				</entry>
      			</map>
    		</property>
    	</bean>
    At least that's how I understand it..

    Dipita

    Comment


    • #3
      If I understand correctly what you're trying to do, try <map merge="true"> on the bean containing the 'parent' reference.

      Comment


      • #4
        Originally posted by gmatthews View Post
        If I understand correctly what you're trying to do, try <map merge="true"> on the bean containing the 'parent' reference.
        I would agree. It's discussed in the reference manual.
        http://www.springframework.org/docs/...ction-elements
        Last edited by karldmoore; Aug 27th, 2007, 04:20 PM.

        Comment


        • #5
          Thanks guys, that was exactly what I wanted to see available!

          But just in case someone doesn't want to read the appropriate docs section - gmatthews made a typo, it's really the "child" element that needs the merge=true attribute.

          Comment

          Working...
          X