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 “” 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 “” 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., we will get the Spring Bean class i.e., which injects 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


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

<?xml version="1.0" encoding="UTF-8"?>

	<!-- enable component scanning -->
	<context:component-scan base-package="com.poc"/>

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


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

package com.poc.bean;

import org.springframework.stereotype.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) class):

package com.poc.bean;

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

import org.springframework.stereotype.Component;


public class CricketTeam {

  private CricketCoach teamCoach;

  public CricketTeam() {

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



  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

package com.poc.test;


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




8. Run the App

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

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