Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bean Definition Inheritance in Spring

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 593
    Comment on it

    Spring Bean Definition Inheritance: Spring Framework provides the facility of Inheritance in it's bean configuration file. Bean configuration consists lots of configuration regarding of bean like bean's id, name, class, properties etc. Spring bean configuration supports Inheritance concept same like we use in bean.

    In bean configuration we can create parent bean and child bean. Child bean can inherit the properties of parent bean and can also override the parent properties. We can create a parent bean which acts as a template and we as many child can share common properties, values and other configuration.

    Spring bean configuration inheritance Example:

    Employee.java

    package com.evon.example.beanDefinitionInheritance;
    public class Employee {
        private String companyName;
        private String empName;
        private String department; 
    
        public String getCompanyName() {
            return companyName;
        }
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
        public String getEmpName() {
            return empName;
        }
        public void setEmpName(String empName) {
            this.empName = empName;
        }
        public String getDepartment() {
            return department;
        }
        public void setDepartment(String department) {
            this.department = department;
        }
    }
    

    Main.java

    package com.evon.example.beanDefinitionInheritance;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    public class Main {
    
        public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/evon/example/beanDefinitionInheritance/bean.xml");
            Employee emp = (Employee)context.getBean("employeeBean");
            System.out.println("Employee Name : "+emp.getEmpName());
            System.out.println("Department : "+emp.getDepartment());
            System.out.println("Company : "+emp.getCompanyName());
        }
    }
    

    bean.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
    
         <bean id="companyBean" class="com.evon.example.beanDefinitionInheritance.Employee">
            <property name="companyName" value="Evon Technologies"/>
        </bean>
        <bean id="employeeBean" parent="companyBean">
            <property name="empName" value="Sumit"/>
            <property name="department" value="IT Department"/>
        </bean>
    </beans>
    

    Output :

    Employee Name : Sumit
    Department : IT Department
    Company : Evon Technologies
    

    In the above bean.xml, we have configured the two bean definitions, one is "companyBean" which is parent and another is "employeeBean" which is child. We are not setting the "companyName" property inside the Child bean, but in output you can see that the value is printed. The property is set in the parent and the child is inheriting that property.

    Here we can instantiate the "companyBean" also, e.g.

    Employee emp = (Employee)context.getBean("companyBean");
    

    But then output will be changed and employee name and department values set as null : Employee Name : null

    Department : null

    Company : Evon Technologies

    The solution for the above problem is that, you can restrict the bean to be instantiate using the "abstract" attribute. Inhritance with Abstract attribute Example:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
    
         <bean id="companyBean" class="com.evon.example.beanDefinitionInheritance.Employee" abstract="true">
            <property name="companyName" value="Evon Technologies"/>
        </bean>
        <bean id="employeeBean" parent="companyBean">
            <property name="empName" value="Sumit"/>
            <property name="department" value="IT Department"/>
        </bean>
     </beans>
    

    Now if you try to instantiate the "companyBean", you will get the following exception :

    Exception in thread "main" org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'companyBean': Bean definition is abstract

    It is not mandatory to define the class attribute in parent bean. As in above example the instantiation of parent class is not necessary, so we can use it to share only common properties and take it as a template. And we can also override those common properties in child class.

    Complete abstraction in bean definition Inheritance:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
         <bean id="companyBean" abstract="true">
            <property name="companyName" value="Evon Technologies"/>
        </bean>
        <bean id="employeeBean" class="com.evon.example.beanDefinitionInheritance.Employee" parent="companyBean">
        <property name="companyName" value="Evon Technologies, Dehradun"/>
            <property name="empName" value="Sumit"/>
            <property name="department" value="IT Department"/>
        </bean>
     </beans>
    

    Output :

    Employee Name : Sumit
    Department : IT Department
    Company : Evon Technologies, Dehradun
    

    In the above example we have override the "companyName" property.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: