Bean definition inheritance

This artcle is taken from the book Getting started with Spring Framework

The following figure shows the dependencies of different application objects in an application:


The above figure shows that the PersonalBankingDao and FixedDepositDao classes are dependent on the DatabaseOperations class. If multiple beans in your application share a common set of configuration (properties, constructor arguments, and so on), you can create a bean definition that acts as a parent for other bean definitions.

The following example listing shows that the PersonalBankingDao and FixedDepositDao bean definitions make use of bean definition inheritance to specify that they are dependent on DatabaseOperations bean:

<bean id="databaseOperations" 
      class="sample.spring.chapter01.springbankapp.utils.DatabaseOperations" /> 

<bean id="daoTemplate" abstract="true"
        <property name="databaseOperations" ref="databaseOperations" /> 
</bean> 

<bean id="fixedDepositDao" parent="daoTemplate" 
      class="sample.spring.chapter01.springbankapp.dao.FixedDepositDaoImpl" /> 

<bean id="personalBankingDao" parent="daoTemplate"
        class="sample.spring.chapter01.springbankapp.dao.PersonalBankingDaoImpl" />

In the above example listing, the daoTemplate bean definition defines the common configuration shared by both the fixedDepositDao and personalBankingDao bean definitions. As both the fixedDepositDao and personalBankingDao bean definitions require the databaseOperations dependency, the daoTemplate bean definition defines the databaseOperations dependency using the <property> element. The <bean> element’s parent attribute specifies the name of the bean definition from which the configuration is inherited. As the parent attribute value is daoTemplate for fixedDepositDao and personalBankingDao bean definitions, they inherit databaseOperations property from the daoTemplate bean definition.

Comments

Popular posts from this blog

Getting started with Spring Framework, Second Edition now available