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 JSR 330 Annotations like
In this tutorial we will look into auto-wiring based on
annotation defined in JSR 330 which is used to inject String values into members of a bean.
Auto-Wiring based on JSR 330 annotations is achieved as follows:
- Use the context:annotation-config tag in spring-context.xml
- Use the @Value annotation defined in JSR 330 to qualify either the member or a corresponding method (usually the setter method) of type String.
Sample Program Overview
The sample program describes the wiring of String values into the Person class.
We will create the Person class with members as name and email.
We will also create the spring-config.xml and declare the person bean.
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 values of name and email into Person bean.
Finally, we will test our setup using TestAutoWireAnnotationValueJSR330 class which will load Spring context and get a reference to Person class. We will print name and email of the Person bean on the console to verify that auto-wiring of
annotation has occured successfully.
Create the Person (see sample code below).
Create members name and email (see lines 8-9 below)
Create accessor methods for name and email (see lines 14-23 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 Person (see lines 16-18 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 TestAutoWireAnnotationValueJSR330 (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 Person class through Spring using the bean name ‘person’ (see line 12 below).
We access the getName() and getEmail() methods of Person class and print the output to verify that auto-wiring of
annotation has occured successfully (see lines 13-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 springautowireannotationvaluejsr330-installer.jar and execute the jar using
java -jar springautowireannotationvaluejsr330-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.