Spring Bean Inheritance Example with parent attribute
Spring Bean Inheritance allows one bean definition to inherit
configuration from another bean using the parent attribute. The child bean
automatically receives all properties from the parent and can add or override specific
values.
In this tutorial, you will build a complete working example with a Car
(parent) and MyCar (child) to demonstrate how Spring Bean Inheritance works
in XML configuration.
What is Spring Bean Inheritance?
Spring Bean Inheritance lets one bean definition inherit configuration
from another bean definition using the parent attribute.
It is important to understand that this is configuration inheritance, not Java class inheritance. The child bean does not extend the parent class in Java — it simply copies properties, scope, and other settings from the parent bean definition.
Key Points
- The
parentattribute points to another bean definition by itsid. - The child bean inherits all properties from the parent.
- The child can override a parent property or add new ones.
- Set
abstract="true"on the parent to prevent Spring from creating an instance of it. - Bean Inheritance is config-only — no Java classes need to extend each other.
- Both parent and child beans can belong to completely unrelated Java classes.
Car.java (Parent Class)
The parent class holds the common properties — name and price
— that every car shares.
package com.deepsingh44.model;
public class Car {
private String name;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
MyCar.java (Child Class)
The child class adds an extra field — avg — that is specific to this
particular car. In a real Spring Bean Inheritance setup, MyCar would
typically extend Car, but Spring Bean Inheritance does not require it — the
beans can be completely unrelated classes.
package com.deepsingh44.model;
public class MyCar {
private String name;
private float price;
private String avg;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getAvg() {
return avg;
}
public void setAvg(String avg) {
this.avg = avg;
}
}
Test.java (Main Class)
Loads the Spring context from spring_config.xml, retrieves the
mycar bean, and prints its name, price, and average.
package com.deepsingh44.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac =
new ClassPathXmlApplicationContext("spring_config.xml");
Object o = ac.getBean("mycar");
MyCar car = (MyCar) o;
System.out.println(car.getName());
System.out.println(car.getPrice());
System.out.println(car.getAvg());
}
}
spring_config.xml
The Spring configuration file. Note the abstract="true" on the parent bean
and the parent="car" attribute on the child bean.
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Parent bean (abstract — no instance created) -->
<bean id="car" class="com.deepsingh44.model.Car" abstract="true">
<property name="name" value="xyz" />
<property name="price" value="450000" />
</bean>
<!-- Child bean inherits from "car" and adds "avg" -->
<bean id="mycar" class="com.deepsingh44.model.MyCar" parent="car">
<property name="avg" value="30" />
</bean>
</beans>
Output
xyz
450000.0
30
The MyCar bean inherited name and price from the
parent car bean, and its own avg field was set to
30.
How It Works (Step-by-Step)
-
Spring loads the XML configuration —
ClassPathXmlApplicationContextreadsspring_config.xmlfrom the classpath. -
Parent bean is marked abstract —
<bean id="car" ... abstract="true">tells Spring not to instantiate it. It exists only as a template. -
Child bean declares parent —
<bean id="mycar" ... parent="car">tells Spring to copy all properties from the parent bean before applying its own. -
Properties are merged —
MyCarends up withname = "xyz",price = 450000, andavg = 30. -
Bean is retrieved by ID —
ac.getBean("mycar")returns the fully configuredMyCarinstance. -
Values are printed — the
Testclass prints the three properties.
Frequently Asked Questions
What is Bean Inheritance in Spring?
Bean Inheritance in Spring means one bean definition can inherit configuration from
another bean definition using the parent attribute. The child bean inherits
properties from the parent and can override or add new ones. It is a
configuration-only inheritance — not Java class inheritance.
What does the abstract="true" attribute do in Spring?
Setting abstract="true" on a bean means Spring will not create an
instance of that bean. It is used purely as a template for other beans to
inherit from. Trying to call getBean() on an abstract bean throws an error.
Can a child bean override a parent bean's property?
Yes. The child bean can redefine any property inherited from the parent bean. The child value takes precedence. This is useful when most beans share common configuration but a few need a different value.
What is the difference between Spring Bean Inheritance and Java class inheritance?
Spring Bean Inheritance is a configuration-level concept — the child bean only inherits property values and other bean settings from a parent bean definition. Java class inheritance happens at the class level where a subclass inherits fields and methods from a superclass. They are completely independent.
Can I inherit from a bean in another XML file in Spring?
Yes. Use the parent attribute with a fully qualified bean name and import
the other XML file using <import resource="..."/>. Alternatively, use
parentBeanFactory for parent-child container relationships.
Conclusion
In this tutorial, you learned how to use Spring Bean Inheritance to
reuse common configuration across multiple beans. The parent attribute
copies all properties from a parent bean, and the child bean can add or override
specific values.
The abstract="true" flag on the parent bean is a best practice — it prevents
accidental instantiation of a template bean that was only meant to be inherited from.
Related topics: Spring Dependency Injection, Spring Setter Injection, Spring Constructor Injection, Spring Bean Scopes, Spring Bean Lifecycle, Spring Annotation Configuration
No comments:
Post a Comment