Spring Method Injection

Concept Overview

In typical software development, classes depend on each other to achieve a desired functionality. e.g. An Automatic Ticket Vending Machine class depends on Ticket class to achieve the functionality of generating tickets.

The dependencies need to be ‘resolved’ before the desired functionality is can be achieved.

When dependency resolution is not performed by the class itself but is left to be done by an external agent (e.g. Spring Framework) it is called dependency injection.

Spring supports 3 kinds of dependency injections:

  1. Constructor Injection
  2. Setter Injection
  3. Method Injection

Method injection is different from Constructor Injection and Setter Injection. While in Constructor and Setter Injection, Spring creates the beans and injects them using the constructor or setter method, in Method Injection Spring overrides a given abstract method of an abstract class and provides an implementation of the overridden method.

This is an advanced form of dependency injection and should be used in very special cases as it involves byte-code manipulation by Spring.

A special case where
Method Injection should be used is when a Prototype bean needs to used within a Singleton
.To undestand this special case in which Method Injection is used, we need to understand the concept of
of a bean.

Spring can instance objects in two ways:

  1. Singleton: Instantiate only one object
  2. Prototype: Instantiate a new object everytime

In Spring terminology,
define the
of the bean.

Spring Singleton And Prototype Trail
for further details.

e.g. There is usually one instance of Ticket Vending Machine (usually available in underground metro railway stations). However, a Ticket Vending Machine generates a new instance of Ticket everytime. In this case, Ticket Vending Machine bean has scope
while Ticket bean has scope

Sample Program Overview

In the sample program, we define a Ticket class and an abstract TicketVendingMachine class.

The TicketVendingMachine class defines an abstract method called generateTicket() which returns a new Ticket. We use Spring’s Method Injection to create a new instance of Ticket (declared as
) from TicketVendingMachine (declared as
). We will configure Spring to override the generateTicket() method and return a new instance of Ticket class.

Finally, we will test this sample by using TestMethodInjection class which will load the TicketVendingMachine bean via Spring and invoke the generateTicket() method.

Required Libraries
  • log4j.jar
  • org.springframework.core.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.beans.jar
  • commons-logging.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.expression.jar
  • cglib.jar

Source Code

Create TicketVendingMachine class

The generateTicket() method is declared as abstract for Spring to perform Method injection (see line 5 below).

Create Ticket class

Declare the TicketVendingMachine and Ticket classes in spring-config.xml (see line 10 and line 15 below).

TicketVendingMachine contains the
tag with the name
and bean id
that tells Spring to perform Method Injection on generateTicket() method and return the Ticket type (see line 12 below).

Ticket is declared with scope Prototype so that a new instance is created for every call to generateTicket() method(see line 17 below)

Finally, we need a java program to test the Method Injection setup.This is done by TestMethodInjection.java (see source code below)

We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans(see line 11 below)

We get a reference of TicketVendingMachine bean (see line 13)

When we invoke the generateTicket() method we get an instance of Ticket class. (see line 14 below)

This Ticket instance was created by Spring using Method Injection.

Running Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run Sample Program
  • Save the springmethodinjection-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment

  • (Alternatively you can go the folder containing the springmethodinjection-installer.jar and execute the jar using
    java -jar springmethodinjection-installer.jar

  • You will see a wizard as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

    1 thought on “Spring Method Injection”

    Leave a Comment