Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring - Injecting Objects By Constructor Injection

In Spring, constructor injection is a method where the Spring container injects the dependencies through the constructor of a bean. It's especially useful when you have mandatory dependencies for a bean, as it ensures that a bean is always instantiated with its required dependencies.

Let's go through the process of injecting objects via constructor injection in Spring:

XML Configuration:

1. Java Classes:

Suppose we have a Person class and an Address class as follows:

public class Address {
    private String city;
    private String country;

    // constructors, getters, setters...
}

public class Person {
    private Address address;

    // Constructor injection for Address
    public Person(Address address) {
        this.address = address;
    }

    // getters, setters, other methods...
}

2. Spring XML Configuration:

<bean id="address" class="com.example.Address">
    <property name="city" value="New York" />
    <property name="country" value="USA" />
</bean>

<bean id="person" class="com.example.Person">
    <constructor-arg ref="address" />
</bean>

Here, the constructor-arg element with the ref attribute tells Spring to pass the address bean as a constructor argument when creating the person bean.

Java-based Configuration:

For Java-based configurations, you can use the @Bean annotation to define the beans and pass the required dependencies in the constructor:

@Configuration
public class AppConfig {

    @Bean
    public Address address() {
        return new Address("New York", "USA");
    }

    @Bean
    public Person person() {
        return new Person(address());
    }
}

In the Java configuration, the person bean is instantiated by invoking its constructor with the address bean.

Annotation-based Configuration:

When using annotation-driven configurations with component scanning, you can achieve constructor injection with the @Autowired annotation:

@Component
public class Address {
    private String city = "New York";
    private String country = "USA";

    // getters, setters, other methods...
}

@Component
public class Person {
    private Address address;

    @Autowired
    public Person(Address address) {
        this.address = address;
    }

    // getters, setters, other methods...
}

With the @Autowired annotation on the constructor, Spring knows to autowire the Address bean when creating the Person bean.

Conclusion:

Constructor injection is a recommended approach when you have required dependencies, as it ensures the bean will never be created without its dependencies. It also promotes immutability, as once the dependencies are set via the constructor, they cannot be changed for that bean instance.

  1. Injecting dependencies with constructor injection in Spring:

    • Description: Constructor injection is a mechanism in Spring where dependencies are provided to a bean through its constructor. This promotes a clear and immutable way of initializing beans with their required dependencies.

    • Code Example:

      public class MyService {
          private final MyRepository myRepository;
      
          // Constructor injection
          public MyService(MyRepository myRepository) {
              this.myRepository = myRepository;
          }
      
          // ...
      }
      
  2. How to pass objects to constructor in Spring:

    • Description: Objects can be passed to a constructor during the instantiation of a bean. This is particularly useful for injecting dependencies and promoting loose coupling between components.

    • Code Example:

      public class MyService {
          private final MyDependency myDependency;
      
          // Constructor with an object parameter
          public MyService(MyDependency myDependency) {
              this.myDependency = myDependency;
          }
      
          // ...
      }
      
  3. Configuring constructor injection with other beans in Spring:

    • Description: Constructor injection allows you to configure a bean with references to other beans. This establishes relationships between components and enables the Spring container to manage their lifecycle.

    • Code Example:

      public class MyService {
          private final AnotherService anotherService;
      
          // Constructor injection with another bean
          public MyService(AnotherService anotherService) {
              this.anotherService = anotherService;
          }
      
          // ...
      }
      
  4. Using @Autowired annotation for constructor injection in Spring:

    • Description: The @Autowired annotation can be used for constructor injection, allowing Spring to automatically resolve and inject the required dependencies.

    • Code Example:

      public class MyService {
          private final MyRepository myRepository;
      
          // Constructor injection using @Autowired
          @Autowired
          public MyService(MyRepository myRepository) {
              this.myRepository = myRepository;
          }
      
          // ...
      }
      
  5. Constructor injection with custom objects in Spring XML configuration:

    • Description: In Spring XML configuration, constructor injection with custom objects is achieved by specifying the object references within the <constructor-arg> elements.

    • XML Configuration Example:

      <bean id="myService" class="com.example.MyService">
          <constructor-arg ref="myRepository" />
      </bean>
      
  6. Injecting dependencies through constructor in Spring:

    • Description: Constructor injection is a recommended approach for injecting dependencies into Spring beans. It ensures that the dependencies are provided at the time of bean creation, leading to a more predictable and testable system.

    • Code Example:

      public class OrderService {
          private final PaymentService paymentService;
      
          // Constructor injection with dependency
          public OrderService(PaymentService paymentService) {
              this.paymentService = paymentService;
          }
      
          // ...
      }
      
  7. Dynamic object injection using placeholders in Spring constructor:

    • Description: Spring allows dynamic object injection using placeholders, which can be resolved from property files or system properties. This provides flexibility in configuring beans with values determined at runtime.

    • Code Example:

      public class MyService {
          private final String serviceName;
      
          // Constructor injection with placeholders
          public MyService(@Value("${service.name}") String serviceName) {
              this.serviceName = serviceName;
          }
      
          // ...
      }
      
  8. Examples of constructor injection with objects in Spring:

    • Example 1:

      public class OrderProcessor {
          private final ShippingService shippingService;
      
          // Constructor injection with another bean
          public OrderProcessor(ShippingService shippingService) {
              this.shippingService = shippingService;
          }
      
          // ...
      }
      
    • Example 2:

      public class ReportGenerator {
          private final DataSource dataSource;
      
          // Constructor injection with a custom object
          public ReportGenerator(DataSource dataSource) {
              this.dataSource = dataSource;
          }
      
          // ...
      }