Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring - Setter Injection with Dependent Object

In Spring, you can perform setter injection with dependent objects (i.e., the injection of one bean into another bean). This is useful when one bean's functionality depends on another bean.

Let's go through an example to demonstrate setter injection with a dependent object:

  1. Java Classes:

    Consider two classes: Engine and Car. The Car class is dependent on the Engine class because a car requires an engine to run.

    // Engine.java
    public class Engine {
        private String model;
    
        public void setModel(String model) {
            this.model = model;
        }
    
        @Override
        public String toString() {
            return "Engine Model: " + model;
        }
    }
    
    // Car.java
    public class Car {
        private Engine engine;
    
        // Setter method for DI
        public void setEngine(Engine engine) {
            this.engine = engine;
        }
    
        public void displayEngineDetails() {
            System.out.println(engine);
        }
    }
    
  2. Spring XML Configuration:

    Create a beans.xml configuration file for defining the beans and their dependencies:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- Engine bean definition -->
        <bean id="engineBean" class="Engine">
            <property name="model" value="V8"/>
        </bean>
    
        <!-- Car bean definition with Engine dependency -->
        <bean id="carBean" class="Car">
            <property name="engine" ref="engineBean"/>
        </bean>
    </beans>
    

    In the XML configuration:

    • The Engine bean is defined with an id of engineBean and a model of "V8".
    • The Car bean is defined with an id of carBean. The engine property of the Car bean references the engineBean.
  3. Test the Configuration:

    Create a main class to load the beans and test the configuration:

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Car car = (Car) context.getBean("carBean");
            car.displayEngineDetails();
        }
    }
    

    Running this main class should display the engine details of the car, which will be "Engine Model: V8".

By following these steps, you can successfully inject a dependent object using setter injection in Spring.

  1. Spring Setter Injection with dependent object example:

    • Description: Demonstrates how to use setter injection to inject a dependent object into a Spring bean.
    • Code:
      public class MyBean {
          private DependencyObject dependencyObject;
      
          public void setDependencyObject(DependencyObject dependencyObject) {
              this.dependencyObject = dependencyObject;
          }
      }
      
  2. Injecting dependent objects using Setter Injection in Spring:

    • Description: Illustrates how to inject dependent objects into a Spring bean using setter injection.
    • Code:
      public class MyBean {
          private AnotherBean anotherBean;
      
          public void setAnotherBean(AnotherBean anotherBean) {
              this.anotherBean = anotherBean;
          }
      }
      
  3. Setter Injection with complex dependencies in Spring Framework:

    • Description: Demonstrates setter injection with complex dependencies, including nested objects and collections.
    • Code:
      public class MyBean {
          private ComplexObject complexObject;
      
          public void setComplexObject(ComplexObject complexObject) {
              this.complexObject = complexObject;
          }
      }
      
  4. Using @Autowired with dependent objects in Spring:

    • Description: Shows how to use @Autowired for injecting dependent objects in Spring using setter injection.
    • Code:
      public class MyBean {
          private AnotherBean anotherBean;
      
          @Autowired
          public void setAnotherBean(AnotherBean anotherBean) {
              this.anotherBean = anotherBean;
          }
      }
      
  5. Setter Injection for injecting dependent objects in Spring XML configuration:

    • Description: Guides on configuring setter injection for injecting dependent objects in Spring XML configuration.
    • Code:
      <bean id="myBean" class="com.example.MyBean">
          <property name="dependencyObject" ref="dependencyObjectBean" />
      </bean>
      
  6. Spring Setter Injection for dependencies with relationships:

    • Description: Illustrates setter injection for injecting dependencies with relationships, such as one-to-one or one-to-many.
    • Code:
      public class MyBean {
          private RelationshipObject relationshipObject;
      
          public void setRelationshipObject(RelationshipObject relationshipObject) {
              this.relationshipObject = relationshipObject;
          }
      }
      
  7. Injecting dependent objects using Setter Injection in Spring Beans:

    • Description: Demonstrates how to inject dependent objects into Spring beans using setter injection.
    • Code:
      @Component
      public class MyComponent {
          private DependentComponent dependentComponent;
      
          @Autowired
          public void setDependentComponent(DependentComponent dependentComponent) {
              this.dependentComponent = dependentComponent;
          }
      }
      
  8. Configuring Setter Injection for dependent objects in Spring components:

    • Description: Guides on configuring setter injection for dependent objects in Spring components.
    • Code:
      @Component
      public class MyComponent {
          private DependencyObject dependencyObject;
      
          @Autowired
          public void setDependencyObject(DependencyObject dependencyObject) {
              this.dependencyObject = dependencyObject;
          }
      }
      
  9. Setter Injection with nested dependencies in Spring:

    • Description: Demonstrates setter injection with nested dependencies, where a bean has dependencies that are themselves complex objects.
    • Code:
      public class MyBean {
          private NestedDependency nestedDependency;
      
          public void setNestedDependency(NestedDependency nestedDependency) {
              this.nestedDependency = nestedDependency;
          }
      }
      
  10. Using @Resource for Setter Injection with dependent objects in Spring:

    • Description: Shows how to use @Resource for setter injection with dependent objects in Spring.
    • Code:
      public class MyBean {
          private AnotherBean anotherBean;
      
          @Resource
          public void setAnotherBean(AnotherBean anotherBean) {
              this.anotherBean = anotherBean;
          }
      }
      
  11. Configuring Spring beans with Setter Injection and dependent objects:

    • Description: Guides on configuring Spring beans with setter injection and dependent objects.
    • Code:
      @Component
      public class MyComponent {
          private DependencyObject dependencyObject;
      
          @Autowired
          public void setDependencyObject(DependencyObject dependencyObject) {
              this.dependencyObject = dependencyObject;
          }
      }
      
  12. Setter-based injection with autowired dependent objects in Spring:

    • Description: Demonstrates setter-based injection with autowired dependent objects in Spring components.
    • Code:
      @Component
      public class MyComponent {
          private AnotherComponent anotherComponent;
      
          @Autowired
          public void setAnotherComponent(AnotherComponent anotherComponent) {
              this.anotherComponent = anotherComponent;
          }
      }
      
  13. Spring Setter Injection for handling complex object graphs:

    • Description: Illustrates how to use setter injection to handle complex object graphs with interrelated dependencies.
    • Code:
      public class MyBean {
          private ComplexObjectGraph complexObjectGraph;
      
          public void setComplexObjectGraph(ComplexObjectGraph complexObjectGraph) {
              this.complexObjectGraph = complexObjectGraph;
          }
      }
      
  14. Setter Injection for injecting dependencies with relationships in Spring:

    • Description: Demonstrates setter injection for injecting dependencies with relationships, such as associations or aggregations.
    • Code:
      public class MyBean {
          private RelationshipObject relationshipObject;
      
          public void setRelationshipObject(RelationshipObject relationshipObject) {
              this.relationshipObject = relationshipObject;
          }
      }
      
  15. Injecting dependent objects using @Value annotation in Spring:

    • Description: Shows how to use @Value annotation for injecting dependent objects in Spring using setter injection.
    • Code:
      public class MyBean {
          @Value("#{dependencyObjectBean}")
          private DependencyObject dependencyObject;
      }
      
  16. Setter-based injection with dependent objects in Spring components:

    • Description: Demonstrates setter-based injection with dependent objects in Spring components.
    • Code:
      @Component
      public class MyComponent {
          private DependentComponent dependentComponent;
      
          @Autowired
          public void setDependentComponent(DependentComponent dependentComponent) {
              this.dependentComponent = dependentComponent;
          }
      }
      
  17. Spring Setter Injection with circular dependencies:

    • Description: Shows how to handle circular dependencies using setter injection in the Spring framework.
    • Code:
      public class MyBeanA {
          private MyBeanB beanB;
      
          public void setBeanB(MyBeanB beanB) {
              this.beanB = beanB;
          }
      }
      
      public class MyBeanB {
          private MyBeanA beanA;
      
          public void setBeanA(MyBeanA beanA) {
              this.beanA = beanA;
          }
      }
      
  18. Handling object graphs in Spring Setter Injection:

    • Description: Guides on handling object graphs (complex structures of interrelated objects) using setter injection in Spring.
    • Code:
      public class MyBean {
          private ObjectGraph objectGraph;
      
          public void setObjectGraph(ObjectGraph objectGraph) {
              this.objectGraph = objectGraph;
          }
      }
      
  19. Setter Injection for injecting prototype-scoped dependent objects in Spring:

    • Description: Demonstrates setter injection for injecting prototype-scoped dependent objects in Spring.
    • Code:
      @Scope("prototype")
      public class PrototypeScopedBean {
          // Prototype-scoped bean implementation
      }
      
      public class MyBean {
          private PrototypeScopedBean prototypeBean;
      
          public void setPrototypeBean(PrototypeScopedBean prototypeBean) {
              this.prototypeBean = prototypeBean;
          }
      }