Typically in Spring, dependency injection is achieved using
However, in large applications, the number of beans will increase and the corresponding XML 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.
Another driving factor is the emergence of number of dependency injection containers besides Spring like Pico-Container, Guice, etc. An effort is underway to standardize the way annotation based auto-wiring is supported by these dependency injection containers so that the application code is loosely coupled to these containers.
The standardization of annotation based dependency injection is provided by
Spring provides auto-wiring based on both XML and Annotations.
In this tutorial we will look into auto-wiring based on Annotations.
Auto-Wiring based on annotations is achieved as follows:
- Use the context:annotation-config tag in spring-context.xml
- Use the @Inject annotation defined in JSR 330 to qualify either the member or a corresponding method (usually the setter method) which takes the injected type as argument
annotation is similar to using Spring’s
annotation. Refer to
for further details.
annotation can be used to qualify:
- a member
- any method (including setter method) which takes the injected type as argument
Note: If Spring finds no bean of injected type for auto-wiring, then it will throw
Also note that if multiple types are available for injection, then Spring will be unable to make a decision on which bean to inject and will throw an Exception.
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 use the
tag in spring-context.xml to let Spring know that annotation based auto-wiring will be used.
We will then use Spring’s auto-wiring feature and use
annotation to inject Printer into ATM bean.
Finally, we will test our setup using TestAutoWireAnnotationJSR330 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-wiring by JSR 330 annotations has occured successfully.
Create the Printer (see sample code below).
Create the printBalanceInformation() method (see line 6 below)
Create the ATM (see sample code below).
Create member printer (see line 7 below)
Create accessor methods for printer (see lines 9-15 below).
annotation on top of the setPrinter() method (see line 12 below). This demonstrates the usage of Annotation based auto-wiring.
Create the printBalanceInformation() method (see line 17 below) and delegate the call to Printer.printBalanceInformation() method (see line 18 below).
Create the spring-config.xml file (see below).
Let Spring know that annotation based auto-wiring will be used by declaring the tag
tag(see line 14 below)
Declare bean for Printer (see lines 16-18 below)
Declare bean for ATM (see line 20 below).
Note that no dependency injection is configured in spring-config.xml file
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 TestAutoWireAnnotationJSR330 (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 using JSR 330 Annotations 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 springautowireannotationjsr330-installer.jar and execute the jar using
java -jar springautowireannotationjsr330-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.