Normally, Spring instantiates a class and performs dependency injection. However, sometimes it may be necessary to instantiate a class via another class (usually called a Factory class). In such a scenario, Spring should not create the class on its own but simply delegate the instantiation to the Factory class.
Spring provides a way to delegate class instantiation to another class by using
tag. Essentially, there is a static method defined in the Factory class that creates the instance of the required bean, hence the name factory-method.
is used in application integration where objects are constructed in a more complex way using a Factory class.
Also, there may be third party libraries that may need to be used within Spring and these third party libraries use Factory classes to instantiate other classes. In such case, the use of
attribute greatly simplifies integrability of Spring based applications with third party libraries.
The following sample code provides an overview of instantiating a class using
Sample Program Overview
The sample program is modelled on the ATM (Automated Teller Machine) system.
We will create an ATM class and Printer class. The ATM class depends on the Printer class to perform printing related responsibilities like printing the balance information for an account.
In this sample, we will assume that creating the instance of Printer involves complex operations and it would be better to use a PrinterFactory class to instantiate the Printer class.
So we will create a PrinterFactory class with static createPrinter() method and returns the Printer type.
We will create the spring-config.xml and use the
attribute to instantiate Printer class using PrinterFactory.createPrinter() method.
Finally, we will test our setup using TestInjectionFactoryMethod class which will load Spring context and get a reference to ATM class. We will print the balance information for an account using the ATM class and verify that the Printer class has been successfully injected into ATM class using
Create the ATM class.
Create a dependency on the Printer class (see line 5 below) with accessor methods (see lines 7-12 below)
The ATM class delegates the call to print the balance information to the Printer class (see line 15 below).
Create the Printer class with a method printBalanceInformation() (see line 5 below).
Create the PrinterFactory class which is a Factory for instantiating Printer class.
Define a static method createPrinter() which returns the Printer class.
Create the spring-config.xml file (see below).
Declare bean for ATM class (see line 10 below). Resolve property by referring to the printer bean (see line 11 below).
Declare a bean with id ‘printer’ (see line 14 below). Note that the class attribute points to PrinterFactory class (see line 15 below).
attribute with value ‘createPrinter’ (see line 16 below).
Note that we are using the PrinterFactory.createPrinter() method to instantiate the Printer class
Finally, we need a java program to test the our setup.This is done by TestInjectionFactoryMethod.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 the reference to ATM class through Spring using the bean name ‘atm’ (see line 12 below).
We access the atm.printBalanceInformation() method and print the output to the console thereby verifying that the Printer class has been successfully injected using
(see line 14 below).
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 springinjectionfactorymethod-installer.jar and execute the jar using
java -jar springinjectionfactorymethod-installer.jar
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.