Pages

March 10, 2017

#Spring Part 2 : Bean Definition Inheritance..

ApplicationContextAware : Beans can get the access to the ApplicationContext object by implementing the ApplicationContextAware interface.


BeanNameAware : This interface is used to get the name of the bean configured in the Spring XML.

Bean Definition Inheritance : We can configure a parent bean definition and have children beans inherit the bean definitions. The child definition can override some values, or add others, as needed.
 e.g:

Suppose the Triangle has three points. Inside Triangle, we have declared variable pointA, pointB and pointC. For bean inheritance, inside the spring.xml, we have added reference of pointA in parenttraingle.

A new bean traingle1 is declared, with a parent tag and reference to pointB and pointC.

When you call context.getBean("traingle1"), it will return a new traingle bean which has reference to pointA, pointB and pointC beans.

 < ?xml version="1.0" encoding="UTF-8"? >
 < !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
 < beans >
     < bean id="parenttraingle" class="com.spring.test.Traingle" >
         < property name="pointA" ref="pointA" / >
     < /bean >
     < bean id="traingle1" class="com.spring.test.Traingle" parent="parenttraingle" >
         < property name="pointB" ref="pointB" / >
         < property name="pointC" ref="pointC" / >
     < /bean >
     < bean id="pointA" class="com.spring.test.Point" >
         < property name="x" value="0" / >
         < property name="y" value="0" / >
     < /bean >
     < bean id="pointB" class="com.spring.test.Point" >
         < property name="x" value="-20" / >
         < property name="y" value="0" / >
     < /bean >
     < bean id="pointC" class="com.spring.test.Point" >
         < property name="x" value="0" / >
         < property name="y" value="20" / >
     < /bean >

 < /beans >

Collection Inheritance
: Now lets suppose, Traingle has a List of Points.
import java.util.List;

public class Traingle {
    private List < Point >  points;
    public void draw()
    {
        System.out.println("Drawing traingle..");
        points.forEach((point)- > System.out.println(point));
    }
    //getters and setter for points  
}

 < ?xml version="1.0" encoding="UTF-8"? >
 < !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
 < beans >
     < bean id="parenttraingle" class="com.spring.test.Traingle" >
         < property name="points" >
             < list >
                 < ref bean="pointA" / >
             < /list >
         < /property >
     < /bean >
     < bean id="traingle1" class="com.spring.test.Traingle" parent="parenttraingle" >
         < property name="points" >
             < list >
                 < ref bean="pointB" / >
                 < ref bean="pointC" / >
             < /list >
         < /property >
     < /bean >
     < bean id="pointA" class="com.spring.test.Point" >
         < property name="x" value="0" / >
         < property name="y" value="0" / >
     < /bean >
     < bean id="pointB" class="com.spring.test.Point" >
         < property name="x" value="-20" / >
         < property name="y" value="0" / >
     < /bean >
     < bean id="pointC" class="com.spring.test.Point" >
         < property name="x" value="0" / >
         < property name="y" value="20" / >
     < /bean >
 < /beans >


If you do this, while initializing the traingle1, it will create a new list and add reference of pointB and pointC. If you want. instead of creating the new list in traingle1, it should update the existing list of parenttraingle then you have to mention 'merge' attricute.

 < bean id="traingle1" class="com.spring.test.Traingle" parent="parenttraingle" >
         < property name="points" >
             < list merge="true" >
                 < ref bean="pointB" / >
                 < ref bean="pointC" / >
             < /list >
         < /property >
 < /bean >


abstract bean: If you do not want to allow Java code to instantiate a particular bean (e.g a parent bean in our case parenttraingle), then set abstract attribute to true. . e.g:
     < bean id="parenttraingle" class="com.spring.test.Traingle" abstract="true" >
         < property name="points" >
             < list >
                 < ref bean="pointA" / >
             < /list >
         < /property >
 < /bean >  



-K Himaanshu Shuklaa..

No comments:

Post a Comment