Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring - Inheriting Bean

In Spring Framework, bean configuration inheritance allows a bean definition to be derived from another bean definition. It can be useful when you want multiple beans to have common properties.

When a bean is inherited, it inherits properties and configuration from its parent bean. However, if the child bean definition has a property with the same name as a property in the parent definition, the child's property will take precedence and override the parent's property.

Note: The parent bean definition cannot be instantiated on its own because it serves as a purely configuration template, and it is recommended to mark them as abstract to avoid accidental instantiation.

XML Configuration Example:

Suppose you have a base bean definition that has common properties:

<bean id="parentBean" abstract="true" class="com.example.BaseClass">
    <property name="commonProperty" value="commonValue" />
</bean>

You can then have child beans that inherit the properties from the parent bean:

<bean id="childBean1" parent="parentBean">
    <property name="specificProperty" value="specificValue1" />
</bean>

<bean id="childBean2" parent="parentBean">
    <property name="specificProperty" value="specificValue2" />
</bean>

In the above example, both childBean1 and childBean2 will have the commonProperty from parentBean as well as their own specificProperty.

Java-based Configuration Example:

In Java-based configuration, you can reuse method configurations to achieve a similar effect:

@Configuration
public class AppConfig {

    @Bean
    public BaseClass parentBean() {
        BaseClass base = new BaseClass();
        base.setCommonProperty("commonValue");
        return base;
    }

    @Bean
    public ChildClass childBean1() {
        ChildClass child = new ChildClass();
        child.setCommonProperty(parentBean().getCommonProperty());
        child.setSpecificProperty("specificValue1");
        return child;
    }

    @Bean
    public ChildClass childBean2() {
        ChildClass child = new ChildClass();
        child.setCommonProperty(parentBean().getCommonProperty());
        child.setSpecificProperty("specificValue2");
        return child;
    }
}

However, keep in mind that the Java-based approach is more of a method re-use than strict inheritance like in the XML configuration.

Conclusion:

Bean inheritance in Spring is useful to avoid redundancy in bean configurations, especially when you have multiple beans sharing common properties. However, use it judiciously to avoid configurations that are difficult to understand and trace.

  1. Using parent beans in Spring framework:

    • Description: Spring allows the creation of parent beans, which can be used as templates for child beans. Child beans inherit properties and configuration from their parent, promoting code reuse and maintainability.

    • Code Example:

      <!-- Parent bean definition -->
      <bean id="parentBean" class="com.example.ParentBean">
          <!-- Properties and configuration -->
      </bean>
      
      <!-- Child bean inheriting from the parent -->
      <bean id="childBean" class="com.example.ChildBean" parent="parentBean">
          <!-- Additional or overridden properties -->
      </bean>
      
  2. Inheriting bean properties in Spring:

    • Description: Child beans inherit properties, dependencies, and configuration from their parent. This allows you to define common settings in a parent bean and reuse them across multiple child beans.

    • Code Example:

      <!-- Parent bean with common properties -->
      <bean id="parentBean" class="com.example.ParentBean">
          <property name="commonProperty" value="Common Value"/>
      </bean>
      
      <!-- Child bean inheriting from the parent -->
      <bean id="childBean" class="com.example.ChildBean" parent="parentBean">
          <!-- Additional or overridden properties -->
      </bean>
      
  3. How to create child beans in Spring:

    • Description: Child beans are created by specifying the parent attribute in the bean definition, referencing the parent bean. The child can then add or override properties of the parent.

    • Code Example:

      <!-- Parent bean definition -->
      <bean id="parentBean" class="com.example.ParentBean">
          <!-- Properties and configuration -->
      </bean>
      
      <!-- Child bean inheriting from the parent -->
      <bean id="childBean" class="com.example.ChildBean" parent="parentBean">
          <!-- Additional or overridden properties -->
      </bean>
      
  4. Inheritance and abstraction in Spring bean configuration:

    • Description: In Spring, bean inheritance supports abstraction and promotes a hierarchical structure in configuration. This helps in organizing and managing beans in a modular and reusable manner.

    • Code Example:

      <!-- Abstract parent bean with common configuration -->
      <bean id="abstractParentBean" abstract="true" class="com.example.AbstractParentBean">
          <!-- Common properties and configuration -->
      </bean>
      
      <!-- Concrete child beans inheriting from the abstract parent -->
      <bean id="childBean1" class="com.example.ChildBean" parent="abstractParentBean">
          <!-- Additional or overridden properties -->
      </bean>
      
      <bean id="childBean2" class="com.example.ChildBean" parent="abstractParentBean">
          <!-- Additional or overridden properties -->
      </bean>
      
  5. Configuring parent and child beans in Spring:

    • Description: Parent and child beans can be configured in the Spring application context XML file, specifying the relationships and properties. This allows for a clear and organized bean hierarchy.

    • Code Example:

      <!-- Parent bean definition -->
      <bean id="parentBean" class="com.example.ParentBean">
          <!-- Properties and configuration -->
      </bean>
      
      <!-- Child bean inheriting from the parent -->
      <bean id="childBean" class="com.example.ChildBean" parent="parentBean">
          <!-- Additional or overridden properties -->
      </bean>
      
  6. Overriding bean properties in Spring inheritance:

    • Description: Child beans can override properties defined in their parent. This provides flexibility and allows customization of specific properties while maintaining the shared configuration.

    • Code Example:

      <!-- Parent bean with common properties -->
      <bean id="parentBean" class="com.example.ParentBean">
          <property name="commonProperty" value="Common Value"/>
      </bean>
      
      <!-- Child bean inheriting from the parent and overriding a property -->
      <bean id="childBean" class="com.example.ChildBean" parent="parentBean">
          <property name="commonProperty" value="Custom Value"/>
          <!-- Additional properties -->
      </bean>
      
  7. Inheriting beans with annotations in Spring:

    • Description: In addition to XML configuration, Spring supports bean inheritance using annotations. The @Component and @Configuration annotations can be used to define parent and child beans.

    • Code Example:

      // Parent bean class with @Component annotation
      @Component
      public class ParentBean {
          // Properties and configuration
      }
      
      // Child bean class inheriting from the parent
      @Component
      public class ChildBean extends ParentBean {
          // Additional or overridden properties
      }