Spring Bean Definition

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Spring IoC container create and manage the objects for the application using the beans. These beans are created with the configuration metadata that you supply to the container in the form of XML bean. The bean definition contains the information called configuration metadata which is needed for the container to know the properties that make the bean definition.

Here are the properties of bean.

  • id
  • class
  • name
  • scope
  • constructor-arg
  • properties
  • autowiring

Spring Framework provides the facility of Inheritance in the bean configuration file. Spring bean configuration supports Inheritance concept in bean. Configuration in the bean you can create parent bean and child bean. Child bean can inherit the properties of parent bean and can also override the parent properties and parent bean acts as a template in the bean and child bean can share common properties, values and other configuration.

Vehicle.java (Parent class)

public class Vehicle {
    private String wheel;
    public String getWheel() {
        return wheel;
    }
    public void setWheel(String wheel) {
        this.wheel = wheel;
    }
}

Bike.java (Child class)

public class Bike {
    private String mirror;
    private String wheel; 
    public String getWheel() {
        return wheel;
    }
    public void setWheel(String wheel) {
        this.wheel = wheel;
    }
    public String getMirror () {
        return mirror;
    }
    public void setMirror (String mirror) {
        this. mirror = mirror;
    }
}

applicationContext.xml (configuration file)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="vehicle"   abstract="true">
    	<property name="wheel" value="Two wheeler"/>
     </bean>
    <bean id="bike" class="com.javatutsworld.inheritance.Bike" parent="vehicle">
    <property name="mirror" value="Two small size mirror"/>    
    </bean>
</beans>

Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
      public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       Bike b = (Bike) context.getBean("bike");
        System.out.println("This output is from Vehicle parent class: "+b.getWheel());
        System.out.println("This output is from Bike child class: "+b.getMirror());
    }
}

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 you can use it to share only common properties and take it as a template. And you can also override those common properties in child class.

Output

 
This output is from Vehicle parent class: Two wheeler
This output is from Bike child class: Two small size mirror
      Advertisements