Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Spring Setter Injection

Spring Setter Injection

This form of Dependency Injection is used very often, especially for optional dependencies and reconfigurable beans. We’ll show how to use it using Spring annotations.

Spring bean to inject as dependency

Let’s specify an interface for dependency:

package com.farenda.spring.tutorial.injection.setter;

public interface BookRepository {
    String titleById(int id);

And the actual Spring Bean implementation that will be injected:

package com.farenda.spring.tutorial.injection.setter;

import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

public class InMemoryBookRepository implements BookRepository {

    // It's our local database ;-)
    private Map<Integer, String> books = new HashMap<>();

        books.put(1, "Effective Java, 2nd edition");
        books.put(2, "Java Concurrency in Practice");
        books.put(3, "Spring in Action");

    public String titleById(int id) {
        return books.get(id);

Spring component with dependency injected using setter

Here we define another Spring Bean, but this time we are going to inject BookRepository through setter, using @Autowired annotation:

package com.farenda.spring.tutorial.injection.setter;

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

public class Library {

    private BookRepository bookRepository;

    public void setBookRepository(BookRepository bookRepository) {
        this.bookRepository = bookRepository;

    public String findBook(int id) {
        return bookRepository.titleById(id);

Constructor or setter injection

It is a good practice to use constructor injection, because it allows to:

  • create beans as immutable objects,
  • make sure all dependencies were provided,
  • return bean to a client in consistent, fully initialized state.

Use setter injection for optional dependencies and/or to allow run-time reconfiguration of beans (e.g. through JMX or a REPL).

Example App using Spring Boot

Here’s the simplest Spring Boot application to run the above code:

package com.farenda.spring.tutorial.injection.setter;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

public class SetterInjection {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication
                .run(SetterInjection.class, args);

        Library library = context.getBean(Library.class);

        System.out.println("Title 1: " + library.findBook(1));
        System.out.println("Title 2: " + library.findBook(2));

The above code produces the following output:

Title 1: Effective Java, 2nd edition
Title 2: Java Concurrency in Practice
Share with the World!