Spring - The purpose of parent attribute.

 The "parent" element of <bean> tag is used for reusing configuration parameters. This "parent" attribute accepts a reference to an another bean of same type. All the parameters that are configured in parent bean are also available in child bean. See the below example.

     
      
            
            
            
            
      
     
      
            
            
      


 In this example I have configured two beans in beans.xml file namely bean1 and bean2. The bean2 have an parent attribute value as "bean1", In this situation the bean2 inherits the all the properties and values of the bean1 (name1, name2, name3, name4 - properties and values). 
 If you want to override the some of the property of bean1 then you have to redefine the same properties into child bean. Here the properties name3 and name4 are override-ed by child bean (bean2). 

Comments