Auto-Wire by type

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 (2) from the above list i.e. Auto-Wire by Type using
byType
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
primary=”false”
bean tag for all the beans that are not intended to be the primary choice for Spring for auto-wire by type and use
primary=”true”
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
autowire-candidate=”false”
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.

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 type using
autowire=”byType”
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
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 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).


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


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

    Your email address will not be published. Required fields are marked *