DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

πŸ“Œspring-note-006: Bean Definition Inheritance

(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>
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ PirateShip.java

import org.springframework.stereotype.Component;

@Component
public class PirateShip extends BaseShip {
    private String shipName = "The Black Pearl";

    public String getShipName() {
        return shipName;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ NavyShip.java

import org.springframework.stereotype.Component;

@Component
public class NavyShip extends BaseShip {
    private String shipName = "The Royal Fortune";

    public String getShipName() {
        return shipName;
    }
}
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Run the Application & Test

πŸ’‘ Run the app using:

mvn spring-boot:run
Enter fullscreen mode Exit fullscreen mode

or

./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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)