Typically in Spring, dependency injection is achieved using
However, in large applications, the number of beans will increase and the corresponding XML to written to configure the numerous beans will become very large and unwieldly.
Spring provides a feature called ‘Auto-Wiring’ that minimizes the XML to be written provided that certain assumptions are made about the nomenclature of beans and properties. Using ‘Auto-Wiring’ feature, the number of tags like
is significantly minimized and the size of XML is reduced considerably for large applications.
Spring provides four kinds of Auto-Wirings:
- Auto-Wire by Name using byName attribute
- Auto-Wire by Type using byType attribute
- Auto-Wire by Constructor for Constructor Injection using constructor attribute
- Auto-Wire by Auto Detect using autodetect attribute
In this trail, we shall focus on the item (2) from the above list i.e. Auto-Wire by Type using
attribute. The rule followed by Spring to resolve dependencies for Auto-Wire by Type:
- Spring attempts to match all properties of the autowired bean with beans that have the same type
- Properties for which there is no matching bean will remain unwired
Usually, auto-wiring by type is helpful only if we don’t have multiple beans of same type. In practice, this is a severe constraint as our application could have multiple types assignable to the same property.
A downside of using auto-wiring by type is that if there are multiple beans with the same type then Spring will throw an exception as it cannot decide which bean to auto-wire. In such cases, we should use the
bean tag for all the beans that are not intended to be the primary choice for Spring for auto-wire by type and use
bean tag for the bean that Spring should consider as the primary candidate for auto-wiring by type.
Also, if we want to eliminate a bean from participating in auto-wiring process itself, then we can set
in the bean tag.
Note that in practice, auto-wire by type is not a preferred way of auto-wiring for application development.
Sample Program Overview
The sample program is based on an ATM system which contains a Printer that prints the balance information for an account.
We will create the Printer class .
We will then create the ATM with member as printer.
We will also create the spring-config.xml and declare the Printer and ATM beans.
We will then use Spring’s auto-wire by type feature to inject Printer into ATM bean.
Finally, we will test our setup using TestAutoWireByType class which will load Spring context and get a reference to ATM class. We will print the balance information for an account number to verify that dependency injection using auto-wire by type has occured successfully.
Create the Printer (see sample code below).
Create the printBalanceInformation() method (see line 5 below)
Create the ATM (see sample code below).
Create member printer (see line 5 below)
Create accessor methods for printer (see lines 7-12 below).
Create the printBalanceInformation() method (see line 14 below) and delegate the call to Printer.printBalanceInformation() method (see line 15 below).
Create the spring-config.xml file (see below).
Declare bean for Printer (see lines 10-12 below)
Declare bean for ATM (see line 14 below)
Auto-Wire by type using
attribute in bean tag (see line 15 below) which demonstrates the usage of Spring Auto-Wire by type feature.
Note that we did not declare the
tag of ATM thereby reducing the XML size.
Finally, we need a java program to test the our setup.This is done by TestAutoWireByType (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 balance information to verify that auto-wiring by type has occured successfully (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 springautowirebytype-installer.jar and execute the jar using
java -jar springautowirebytype-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.