Wednesday, 20 April 2016

How to reuse beans using inheritance ?


Spring offers an inheritance-like mechanism to reduce the duplication of configuration information and make the XML configuration simpler.

A child bean definition can inherit configuration information from its parent bean, which essentially serves as a template for the child beans.

You need to specify abstract=true for the parent bean, and the parent reference in the child bean.

Example
<bean id="abstractService" abstract="true“  
      class="com.xyz.AbstractService">
   <property name="companyName" value="xyz1" />
</bean>
<bean id="shippingService" parent="abstractService"
      class="com.xyz.ShippingService">
  <property name="shippedBy" value="xyz2" />
</bean>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.