Results 1 to 5 of 5

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

  1. #1
    Join Date
    Jun 2007
    Posts
    6

    Default 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 at 04:42 PM.

  2. #2

    Default

    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

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

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

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

    Default

    Quote 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 at 04:20 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
    Jun 2007
    Posts
    6

    Default

    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.

Posting Permissions

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