Springs - SPLessons

Chapter 9

Spring Auto Wiring

Spring Auto Wiring

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.

B.java
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");}  
}  

A.java
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(){  
    print();  
    b.print();  
}  
}  

Test.java
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);  
    a.display();  
}  
}  

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.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
<bean id="b" class="splesson.B"></bean>  
<bean id="a" class="splesson.A" autowire="byName"></bean>  
  
</beans>  

Output
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>  

Summary

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