Spring Bean Definition Inheritance Example

By | June 22, 2021

In this post, We will learn about the Spring Bean Definition Inheritance Example using a Demo Project.

A child bean definition can inherit configuration data from a parent definition. The child definition can also override some properties or add others as needed.

Spring Framework Bean’s definition of inheritance has nothing to do with Java class inheritance but the inheritance concept is almost similar. We can define a parent bean definition as a template and other child Spring beans can inherit the required configuration from the parent bean.

pom.xml

Employee.java

applicationContext.xml

When We use XML-based configuration then a child bean definition can refer to the parent attribute, specifying the parent bean as the value of this attribute.

We can usually create a Bean definition template and that can be used by other child bean definitions without putting much effort. While defining a Bean Definition Template, We should not specify the class attribute and should specify an abstract attribute with a value of true as shown in the following code snippet −

ClientTest.java


If you run ClientTest.java as Java Application then it will give the below output: 

Employee [id=1001, name=KK, gender=Male, departmentName=IT, salary=80000.0]

That’s all about Spring Bean Definition Inheritance Example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.