Auto-Wire by name

Pre-requisite trails

Spring Setter Injection
Spring Constructor Injection

Concept Overview

Typically in Spring, dependency injection is achieved using
bean
,
constructor
and
property
tags.

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
constructor
and
property
is significantly minimized and the size of XML is reduced considerably for large applications.

Spring provides four kinds of Auto-Wirings:

  1. Auto-Wire by Name using byName attribute
  2. Auto-Wire by Type using byType attribute
  3. Auto-Wire by Constructor for Constructor Injection using constructor attribute
  4. Auto-Wire by Auto Detect using autodetect attribute

In this trail, we shall focus on the item (1) from the above list i.e. Auto-Wire by Name using
byName
attribute. The rule followed by Spring to resolve dependencies for Auto-Wire by Name:

  • Spring attempts to match all properties of the autowired bean with beans that have the same name (or ID)
  • Properties for which there is no matching bean will remain unwired

In practice, it usually the case that the name of the property and the name of the bean intended to be wired into that property is identical. Hence, auto-wiring by name is the most common of all forms auto-wirings provided by Spring.

A downside of using this kind of auto-wiring is that the team needs to ensure that proper naming conventions are followed for property and bean names. Also, declaring multiple beans with same name should be avoided.

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 name feature to inject Printer into ATM bean.

Finally, we will test our setup using TestAutoWireByName 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 name 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 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 name using
autowire=”byName”
attribute in bean tag (see line 15 below) which demonstrates the usage of Spring Auto-Wire by name feature.

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 TestAutoWireByName (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 name 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 springautowirebyname-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springautowirebyname-installer.jar and execute the jar using
    java -jar springautowirebyname-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
    springautowirebyname
    . 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