Back to: Spring Framework Tutorials
Spring Framework Bean Definition Inheritance
In this article, I am going to discuss Spring Framework Bean Definition Inheritance with Examples. Please read our previous article, where we discussed Spring Framework Bean Post Processors.
What is Spring Framework Bean Definition Inheritance?
In the Spring framework, bean definition interfaces provide a way to define and configure beans within the Spring container. These interfaces define the necessary methods and properties to specify how beans should be instantiated, configured, and managed by the container.
Important bean definition interfaces in Spring:
- BeanDefinition: The core interface for defining a bean. It represents the configuration metadata for a bean, including its class, scope, lifecycle callbacks, dependencies, and more. The BeanDefinition interface provides methods to set and retrieve these properties, allowing developers to define beans programmatically.
- AbstractBeanDefinition: An abstract implementation of the BeanDefinition interface that provides additional features and flexibility. It serves as a base class for concrete bean definition classes, such as GenericBeanDefinition and RootBeanDefinition.
- GenericBeanDefinition: A flexible implementation of AbstractBeanDefinition that allows defining beans without specifying a particular class. It provides methods to set the bean’s class name, constructor arguments, properties, and other configuration details. This interface is often used when the bean class needs to be determined dynamically at runtime.
- RootBeanDefinition: Another concrete implementation of AbstractBeanDefinition that represents the most commonly used bean definition type. It allows specifying the bean’s class directly and provides methods for setting properties, constructor arguments, and other configuration options.
- ChildBeanDefinition: A specialized bean definition that represents a child bean inheriting configuration from its parent. It allows for overriding or extending the configuration of the parent bean definition, providing a way to create variations of a bean definition without duplicating all the configuration details.
These bean definition interfaces play a crucial role in the Spring framework. They enable developers to define beans declaratively or programmatically, controlling various aspects such as the bean’s class, scope, dependencies, and lifecycle callbacks. Bean definition interfaces serve as the foundation for creating bean instances within the Spring container, allowing for efficient management and configuration of the application’s components.
By leveraging these interfaces, developers can effectively structure and organize their beans, define complex dependencies, and achieve loose coupling and modularity within their applications. The Spring container uses these bean definition interfaces to create, initialize, and manage beans based on the provided configuration, resulting in a flexible and highly configurable application architecture.
Implementing Bean Definition Interfaces in Spring Framework
Before starting the project, ensure that the required JAR files are in the project’s “Referenced Libraries” folder.
Step 1: Create a new file called Student.java in the src/ directory. Add the following lines of code to the file:
public class Student { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
This class represents a POJO class which will be used to store a student. Note that the constructor, toString, setters, and getters can be automatically added using the “Source Action…” option in the right-click menu of VS Code.
Step 2: Create a new file called Grade.java in the src/ directory. Add the following lines of code to the file:
public class Grade { private int id; private String name; private char grade; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getGrade() { return grade; } public void setGrade(char grade) { this.grade = grade; } @Override public String toString() { return "Grade [id=" + id + ", name=" + name + ", grade=" + grade + "]"; } }
This class represents a POJO class which will be used to store the grade of a particular student. Ensure that the variable names “id” and “name” are the same as in Student.java. This is critical for bean inheritance. Note that the constructor, toString, setters and getters can be automatically added using the “Source Action…” option in the right-click menu of VS Code.
Step 3: In the src/ directory, there must be a file called App.java. Typically, this file is created automatically (when the project is created). This file contains the main() function. Modify the file as follows:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Student s = (Student) context.getBean("student1"); Grade g = (Grade) context.getBean("grade1"); System.out.println(s.toString()); System.out.println(g.toString()); } }
The main function creates a student object and a grade object from the beans.xml file. It then prints out the objects to the terminal.
Step 4: Create a new file called Beans.xml in the src/ directory. Add the following lines of code to the file:
<?xml version = "1.0" encoding = "UTF-8"?> <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 = "student1" class = "Student"> <property name = "id" value = "1"/> <property name = "name" value = "Student A"/> </bean> <bean id = "grade1" class = "Grade" parent = "student1"> <property name = "grade" value = 'B'/> </bean> </beans>
This file provides the parameters to create an object of the type Student and another object of the type Grade. Note that in the Grade object, we have not provided all the attributes. Rather, we have marked the parent class as student1. This means that grade 1 will inherit all of the attributes of student 1. It may then choose to keep the attributes as is (as we have done in this case) or override them.
Step 5: Compile and execute the application. Ensure compilation is successful. Ensure that the output is as expected:
Congratulations! You have completed the bean definition inheritance application in Spring Framework!
In the next article, I am going to discuss Spring Framework Dependency Injection. Here, in this article, I try to explain Spring Framework Bean Definition Inheritance with Examples. I hope you enjoy this Spring Framework Bean Definition Inheritance article.