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:
- Constructor Injection
- Setter Injection
- 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
scope
of a bean.
Spring can instance objects in two ways:
- Singleton: Instantiate only one object
- Prototype: Instantiate a new object everytime
In Spring terminology,
Singleton
and
Prototype
define the
scope
of the bean.
See
Spring Singleton And Prototype Trail
for further details.
Singleton
while Ticket bean has scope
Prototype
.
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
Prototype
) from TicketVendingMachine (declared as
Singleton
). 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.
- 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
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
lookup-method
tag with the name
generateTicket
and bean id
ticket
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.
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.
(Alternatively you can go the folder containing the springmethodinjection-installer.jar and execute the jar using
java -jar springmethodinjection-installer.jar
command)
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
springmethodinjection
. 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.
Thank. Simple and affordable.