Springs - SPLessons

Spring Auto Wiring

Chapter 10

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring Auto Wiring

Spring Auto Wiring

shape Description

The functionality of Spring Auto Wiring is that enables the developer to inject the object dependency implicitly, it utilizes setters and constructor injection. The disadvantage of auto wiring is that it can not be utilized to inject primitive and string values. The advantage of auto wiring is less coding. Following is the syntax to declare the Spring Auto Wiring framework.

<bean id="a" class="org.splesson.A" autowire="byName"></bean>

Where byName is the mode of auto wiring, Following are the different modes of autowiring.

Modes Description
no Default mode.
byName To inject the object dependency  regarding name of the bean.
byType To inject the object dependency  regarding type of the bean
constructor To inject the dependency by calling constructor class.

shape Example

This class contains a constructor and method only.

package splesson;

public class B {  
B(){System.out.println("b is created");}  
void print(){System.out.println("hello b");}  

This class contains reference of B class and constructor and method.

package splesson;

public class A {  
B b;  
A(){System.out.println("a is created");}  
public B getB() {  
    return b;  
public void setB(B b) {  
    this.b = b;  
void print(){System.out.println("hello a");}  
void display(){  

This class gets the bean from the applicationContext.xml file and calls the display method.

package splesson;

import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class Test {  
public static void main(String[] args) {  
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");  
    A a=context.getBean("a",A.class);  

The Application Context is spring’s more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface.


<?xml version="1.0" encoding="UTF-8"?>  
<bean id="b" class="splesson.B"></bean>  
<bean id="a" class="splesson.A" autowire="byName"></bean>  

When compile the code following is the output will be generated.

b is created
a is created
hello a
hello b

byName autowiring mode

In case of byName autowiring mode, bean id and reference name must be same.It internally uses setter injection.

<bean id="b" class="splesson.B"></bean>  
<bean id="a" class="splesson.A" autowire="byName"></bean>  

But, if you change the name of bean, it will not inject the dependency as follows.

<bean id="b1" class="splesson.B"></bean>  
<bean id="a" class="splesson.A" autowire="byName"></bean>  


shape Key Points

  • Spring Auto Wiring – Constructor mode of auto wiring calls large number of parameters.
  • Spring Auto Wiring – byName and byType modes internally use setter methods.