Spring Setter Dependency Injection

In this topic, we are using the setter dependency injection technique to inject values through the setter method. This project is a Maven-Based Spring Project and contains the following files.

  • BankApp.java

  • AppConfig.java

  • Manager.java

  • Accontant.java

  • Employee.java

  • pom.ml

And the following is a maven project structure created for the Spring application.

Project Structure:

The files created into the above project contains the following code. See the files below.

Files Source Code:


This file contains the code to create an IOC container for our application. The AnnotationConfigApplicationContext class is used to create an object for application context.

package com.studytonight.community;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BankApp {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		Manager manager = context.getBean(Manager.class);

// AppConfig.java

This is a configuration file in Java which is an alternate of applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @ComponentScan annotation is used to indicate the component location in our spring project.

package com.studytonight.community;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

public class AppConfig {


// Manager.java

This is another component class that is marked using the @Component annotation and implements the Employee interface. In this class, we are implementing a setter dependency injection. See, the Manager class calls a method of Accountant class by using the Accountant class object which is instantiated inside a setter method. See the example below.

package com.studytonight.community;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class Manager implements Employee{
	Accountant accountant;
	public void setAccountant(Accountant accountant) {
		this.accountant = accountant;
	public void doWork() {
		System.out.println("Manage the branch office");
	public void callMetting() {

// Accountant.java

This is a component class that is marked using @Component annotation. It implements the Employee interface and overrides its method doWork().

package com.studytonight.community;
import org.springframework.stereotype.Component;
public class Accountant implements Employee{
	public Accountant() {
		System.out.println("Inside Accountant Constructor");
	public void doWork() {	
		System.out.println("Audit the accounts...");

// Employee.java

This is an interface Employee that contains a doWork() abstract method. Each class that implements this interface will have to override the doWork() method.

package com.studytonight.community;

public interface Employee {
	void doWork();


// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->


Run the Application

After successfully completing the project and adding the dependencies run the application and you will get the output as below.

Inside Accountant Constructor
Audit the accounts...

Configuration using XML

The above project is configured using Java code only. No XML configuration did there but we can configure it with XML code as well. We just need to create a file applicationContext.xml and read into the BankApp class. The applicationContext.xml file contains the following code.

// applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

	<bean id="accountant"
		class="com.studytonight.community.Accountant" />
	<bean id="manager" class="com.studytonight.community.Manager">
		<property name="accountant">
			<ref bean="accountant" />