(Reference: Spring Docs - Bean Definition Inheritance)
πΉ What is Bean Definition Inheritance?
π‘ Spring allows one bean to inherit properties from another bean.
π‘ Useful for reusing common configurations across multiple beans.
π‘ Implemented via XML (parent
attribute) or Java-based configuration (@Bean
inheritance).
π How to Implement Bean Definition Inheritance
1οΈβ£ XML-Based Bean Inheritance (Legacy)
β In traditional XML configuration, you can define a parent bean with common properties and let child beans inherit them.
π Example (XML Configuration - Not Recommended for Modern Apps)
<bean id="baseShip" abstract="true">
<property name="captain" value="Blackbeard"/>
<property name="crewSize" value="100"/>
</bean>
<bean id="pirateShip" parent="baseShip">
<property name="shipName" value="The Black Pearl"/>
</bean>
<bean id="navyShip" parent="baseShip">
<property name="shipName" value="The Royal Fortune"/>
</bean>
π₯ Both pirateShip
and navyShip
inherit properties from baseShip
!
2οΈβ£ Java-Based Bean Inheritance
β Modern Spring applications use Java-based configuration instead of XML.
β Use inheritance in Java classes to achieve the same effect.
π Example: Abstract Parent Bean
public abstract class BaseShip {
protected String captain = "Blackbeard";
protected int crewSize = 100;
public String getCaptain() {
return captain;
}
public int getCrewSize() {
return crewSize;
}
}
π Child Beans Inheriting Properties
import org.springframework.stereotype.Component;
@Component
public class PirateShip extends BaseShip {
private String shipName = "The Black Pearl";
public String getShipName() {
return shipName;
}
}
@Component
public class NavyShip extends BaseShip {
private String shipName = "The Royal Fortune";
public String getShipName() {
return shipName;
}
}
π₯ Both PirateShip
and NavyShip
automatically inherit captain & crew size from BaseShip
!
3οΈβ£ Using @Bean
Methods for Inheritance
β You can also define beans using @Configuration
and have them inherit properties.
π Example: Java Config with @Bean
Inheritance
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ShipConfig {
@Bean
public BaseShip baseShip() {
return new BaseShip();
}
@Bean
public PirateShip pirateShip() {
PirateShip ship = new PirateShip();
ship.captain = baseShip().getCaptain();
ship.crewSize = baseShip().getCrewSize();
return ship;
}
@Bean
public NavyShip navyShip() {
NavyShip ship = new NavyShip();
ship.captain = baseShip().getCaptain();
ship.crewSize = baseShip().getCrewSize();
return ship;
}
}
π₯ This approach ensures that all ships share common properties while allowing modifications!
π Hands-On Project: Testing Bean Inheritance
π‘ Letβs modify our Spring Boot app to test bean inheritance!
Step 1: Create Parent and Child Beans
π BaseShip.java
public abstract class BaseShip {
protected String captain = "Blackbeard";
protected int crewSize = 100;
public String getCaptain() {
return captain;
}
public int getCrewSize() {
return crewSize;
}
}
π PirateShip.java
import org.springframework.stereotype.Component;
@Component
public class PirateShip extends BaseShip {
private String shipName = "The Black Pearl";
public String getShipName() {
return shipName;
}
}
π NavyShip.java
import org.springframework.stereotype.Component;
@Component
public class NavyShip extends BaseShip {
private String shipName = "The Royal Fortune";
public String getShipName() {
return shipName;
}
}
Step 2: Create a REST Controller to Test Inheritance
π ShipController.java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ships")
public class ShipController {
private final PirateShip pirateShip;
private final NavyShip navyShip;
public ShipController(PirateShip pirateShip, NavyShip navyShip) {
this.pirateShip = pirateShip;
this.navyShip = navyShip;
}
@GetMapping("/pirate")
public String getPirateShip() {
return "π΄ββ οΈ Pirate Ship: " + pirateShip.getShipName() +
", Captain: " + pirateShip.getCaptain() +
", Crew: " + pirateShip.getCrewSize();
}
@GetMapping("/navy")
public String getNavyShip() {
return "β Navy Ship: " + navyShip.getShipName() +
", Captain: " + navyShip.getCaptain() +
", Crew: " + navyShip.getCrewSize();
}
}
Step 3: Run the Application & Test
π‘ Run the app using:
mvn spring-boot:run
or
./mvnw spring-boot:run
π Test Bean Inheritance in Browser:
1οΈβ£ Check the Pirate Ship:
http://localhost:8080/ships/pirate
-
Response:
π΄ββ οΈ Pirate Ship: The Black Pearl, Captain: Blackbeard, Crew: 100
2οΈβ£ Check the Navy Ship:
http://localhost:8080/ships/navy
-
Response:
β Navy Ship: The Royal Fortune, Captain: Blackbeard, Crew: 100
π₯ Boom! You just witnessed Bean Definition Inheritance in action!
π Summary of Step 6
β
Bean Definition Inheritance allows reusing common bean configurations.
β
Implemented via XML (parent
attribute) or Java-based configuration (@Bean
).
β
Java-based inheritance is preferred for modern Spring Boot applications.
β
Best practice: Use abstract base classes for shared properties & behavior.
π Topics Covered in This Section
π Bean Definition Inheritance (βοΈ Covered)
- XML-based inheritance (parent attribute) β for legacy projects.
- Java-based inheritance (extending base class) β recommended modern approach.
- Using @bean configuration for inheritance.
π οΈ Spring Boot & Bean Inheritance (βοΈ Covered)
- Best practices for using inheritance in real-world applications.
- How to share properties across multiple bean definitions.
- Differences between XML-based and Java-based configurations.
π₯ Key Takeaways You Need to Remember:
β
Bean Definition Inheritance allows reusing properties in multiple beans.
β
XML-based inheritance (parent) is for legacy projects; modern apps use Java-based.
β
Best practice: Use abstract base classes to share logic across beans.
β
Use @bean methods for more flexibility when defining bean inheritance.
Top comments (0)