Setter Injection with Annotation

The “@Autowired” annotation is given to perform the byType mode of auto wiring. This annotation can be applied at the field level, setter method level, and arbitrary method level. We can use “@Component” and “@Autowired”.The “@Compotent”  annotation is used for registering a java class as Spring Bean and the “@Autowired” annotation to inject a Dependent class to the Target class by using the setXXX(-) method.

In the example, we will avoid <property> and <bean id= “bean_id_name” class= “ package”/> to configure in the XML configuration file. We will use the “@Autowired” annotation for setter injection and then Spring container inject dependent bean class. The “@Component” annotation for Spring bean configuration then the Spring container automatically registers the class which is annotated with this annotation. The“<context:component-scan base-package=”base_package_name”/>” for configuring the base into the XML configuration file. So, the Spring Container scans the base package of the project.

In this example, the “CricketCoach.java” POJO class is a dependent class that is annotated “@Component” annotation. This contains a 0-parameter constructor and method. The 0-parameter constructor contains just prints a message when it is executed. The method contains printing a message. The “CricketTeam.java” POJO class is a target class that is annotated with “@Component” and “@Autowired” annotations. This contains a 0-parameter constructor which is executed when it is instantiated and the setter method to pass a parameter of CricketCoach type reference. The “applicationContext.xml” file contains this tag “<context:component-scan base-package=” com.poc”/>” to enable scanning component annotated class as Spring bean class of the application. The main class i.e., ClientApp.java we will get the Spring Bean class i.e., CricketTeam.java which injects CricketCoach.java class to this class with the @Autowired by calling its setter method and instantiated with 0-param constructor by the Spring Container. So, thus we can access the method of the Spring Bean class and print output on the console.

Development Process:
     1. Keep eclipse IDE ready
     2. Project Structure
     3. Enable component scanning in the Spring XML config file for the example of the setter injection configuration with annotation
     4. Add the jar file to the Build Path of the Project for the example of the setter injection configuration with annotation
     5. Create a Dependent Bean Class(@Component)
     6. Create a Target Bean Class(@Component and @Autowired)
     7. Create Main Class
     8.  Run the App

1. Keep eclipse IDE ready
2. Project Structure

setter_injection_in_annotation

3. Enable component scanning in the Spring XML config file for the example of the setter injection configuration with annotation
applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- enable component scanning -->
	<context:component-scan base-package="com.poc"/>
</beans>

4. Add the jar file to the Build Path of the Project for the example of the setter injection configuration with annotation
Right Click on Project(Setter_Injection_With_Annotation) ⇒ Build Path ⇒ Java Build Path ⇒ libraries ⇒ Add External jars

Commons-logging-<version>.jar
spring-beans-<version>.jar
spring-context-<version>.jar
spring-context-support-<version>.jar
spring-core-<version>.jar
spring-expression-<version>.jar
spring-aop-<version>.jar

5. Create a Dependent Bean Class(@Component)
CricketCoach.java(Pojo Class): 

package com.poc.bean;

import org.springframework.stereotype.Component;

@Component

public class CricketCoach {

  public CricketCoach() {

    System.out.println("Constructor: 0-param constructor call of CricketCoach");

  }

  public String getDailyWorkout() {

    return "4 hours of Practice";

  }

}

6. Create a Target Bean Class(@Component and @Autowired)
CricketTeam.java(Pojo class):

package com.poc.bean;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

@Component

public class CricketTeam {

  private CricketCoach teamCoach;

  public CricketTeam() {

    System.out.println("Constructor: 0-param constructor is called(CricketTeam)");

  }

  @Autowired

  public void setTeamCoach(CricketCoach teamCoach) {

    System.out.println("The Coach: inside setter method-setTheCoach");

    this.teamCoach = teamCoach;

  }

  public CricketCoach getTeamCoach() {

    return teamCoach;

  }

  public String getInstruction() {

    return "Coach Instruction:----" + teamCoach.getDailyWorkout();

  }

}

→ In this class “@Component” annotation, the Spring container creates a “CricketTeam” class object using a 0-param constructor and injects “CricketCoach” by using setter injection.

7. Create Main Class
ClientApp.java:

package com.poc.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.poc.bean.CricketTeam;

public class ClientApp {

  public static void main(String[] args) {

    // load the spring Configuration file

    ClassPathXmlApplicationContext context =

      new ClassPathXmlApplicationContext("applicationContext.xml");

    //retrieve bean from spring container

    CricketTeam theTeam = context.getBean("cricketTeam", CricketTeam.class);

    // call the method on bean

    System.out.println(theTeam.getInstruction());

    context.close();

  }
}

8. Run the App
Output:

Constructor: 0-param constructor call of CricketCoach
Constructor: 0-param constructor is called(CricketTeam)
The Coach: inside setter method-setTheCoach
Coach Instruction:----4 hours of Practice

Conclusion:
This example is explained How to configure Setter Injection with Annotation and XML config file? How to use @Autowired in Setter Injection? How to use @Component in Setter Injection?

Leave a Comment