Spring Auto-Wiring Using Annotations with Qualifiers

Concept Overview

Typically in Spring, dependency injection is achieved using
bean
,
constructor-arg
and
property
tags within XML configuration file.

However, in large applications, the number of beans increases 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
constructor-arg
and
property
is significantly minimized and the size of XML is reduced considerably for large applications.

Spring provides auto-wiring based on both XML and Annotations.

In this tutorial we will look into auto-wiring based on Annotations using Qualifiers to specify the bean name to be injected.

Auto-Wiring based on annotations is achieved as follows:

  • Use the context:annotation-config tag in spring-context.xml
  • Use the @Autowired annotation to qualify either the member or a corresponding method (usually the setter method) which takes the injected type as argument
  • Use the @Qualifier(name=”..”) annotation to provide the name of the bean to be used by Spring for dependency injection

Using
@Autowired
annotation is similar to using XML auto-wire configuration using
byType
. Refer to
this trail
for further details.

The
@Autowired
annotation can be used to qualify:

  • a member
  • any method (including setter method) which takes the injected type as argument

If multiple bean types are available for injection, then Spring will be unable to make a decision on which bean to inject and will throw an Exception. In such cases, we can use the
@Qualifier(name=”..”)
annotation and give the name of the bean that we want Spring to inject. This is equivalent to
auto-wiring by name
using XML.

Note: If Spring finds no bean of injected type for auto-wiring, then it will throw
NoSuchBeanDefinitionException
. If in such a situation, we don’t want Spring to throw the exception and it is acceptable to leave the dependency un-injected then use
@Autowired(required=false)
.

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
context:annotation-config
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
@Autowired
and
@Qualifier
annotations to inject Printer into ATM bean.

Finally, we will test our setup using TestAutoWireAnnotationQualifier 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 by auto-wiring by Annotations using Qualifiers has occured successfully.

Required Libraries
  • commons-logging.jar
  • log4j.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.beans.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.core.jar
  • org.springframework.expression.jar



Source Code

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 8 below)

Create accessor methods for printer (see lines 10-17 below).

Add the
@Autowired
annotation for annotation based auto-wiring on top of the setPrinter() method (see line 13 below).

Also add the
@Qualifier(name=”printer”)
annotation for dependency injection based on name on top of the setPrinter() method (see line 14 below). This demonstrates the usage of Annotation based auto-wiring using Qualifiers.

Create the printBalanceInformation() method (see line 19 below) and delegate the call to Printer.printBalanceInformation() method (see line 20 below).

Create the spring-config.xml file (see below).

Let Spring know that annotation based auto-wiring will be used by declaring the tag
context:annotation-config
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
property
tag with
bean
tag of ATM thereby reducing the XML size.


Finally, we need a java program to test the our setup.This is done by TestAutoWireAnnotationQualifier (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 Annotations has occured successfully (see line 14 below).


Running Sample Program

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.

Download And Automatically Run Sample Program
  • Save the springautowireannotationqualifier-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springautowireannotationqualifier-installer.jar and execute the jar using
    java -jar springautowireannotationqualifier-installer.jar
    command)

  • You will see a wizard as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    springautowireannotationqualifier
    . 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.

    Leave a Comment