Monday, April 28, 2014

Spring Beans Auto-Wiring

We can configure Spring beans dependencies using <property>  and <consturctor-arg>.But Spring container can auto wire relationships between collaborating objects without using the above mentioned tags.This will allow us to minimize the amount of XML configuration in our Spring Context xml file.But we need to be very careful using auto wiring as this will be difficult if some thing goes wrong and new developers in the team hard to debug how the beans auto wired.

There are 4 ways we can do the bean auto wiring.By default there is no auto wiring and we need to explicitly mention if we need to let the container know how our beans should be auto wired.

1)byName
2)byType
3)constructor
4)autodetect

Lets see each type in action

1)byName: auto wiring by name. If we defined auto wiring byName in bean definition file then Spring container looks at the properties of the bean and tries to match the name of the property and if both matches it wire its properties.

Lets see in action.For example if i have a Employee and Address classes and Address is declared as an instance field in Employee class.

Employee.java


package com.ramesh.domain;  
 public class Employee {  
      private int id;  
      private String name;  
      private long salary;  
      private Address address;  
      public int getId() {  
           return id;  
      }  
      public void setId(int id) {  
           this.id = id;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public long getSalary() {  
           return salary;  
      }  
      public void setSalary(long salary) {  
           this.salary = salary;  
      }  
      public Address getAddress() {  
           return address;  
      }  
      public void setAddress(Address address) {  
           this.address = address;  
      }  
 }  

Address.java


package com.ramesh.domain;  
 public class Address {  
      private String street;  
      private String pinCode;  
      private String city;  
      public String getStreet() {  
           return street;  
      }  
      public void setStreet(String street) {  
           this.street = street;  
      }  
      public String getPinCode() {  
           return pinCode;  
      }  
      public void setPinCode(String pinCode) {  
           this.pinCode = pinCode;  
      }  
      public String getCity() {  
           return city;  
      }  
      public void setCity(String city) {  
           this.city = city;  
      }  
 }  


 spring-context.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:mvc="http://www.springframework.org/schema/mvc"  
      xmlns:context="http://www.springframework.org/schema/context"  
      xsi:schemaLocation="  
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
      <bean id="employee" class="com.ramesh.domain.Employee" autowire="byName">  
           <property name="id" value="844" />  
           <property name="name" value="Ramesh M" />  
           <property name="salary" value="85000" />  
      </bean>  
      <bean id="address" class="com.ramesh.domain.Address">  
           <property name="street">  
                <value>Embassy Golflinks Business Park Street</value>  
           </property>  
           <property name="city">  
                <value>Bangalore</value>  
           </property>  
           <property name="pinCode">  
                <value>560071</value>  
           </property>  
      </bean>  
 </beans>  


If you notice these lines in our xml file, we didn't mention the address either as a property or a constructor-arg.We mentioned another attribute autowire="byName" for which the Spring container will check a bean name "address" and match both the property mentioned in employee bean and xml file and autowires them

<bean id="employee" class="com.ramesh.domain.Employee" autowire="byName">
<property name="id" value="844" />
<property name="name" value="Ramesh M" />
<property name="salary" value="85000" />
</bean>

I will explain about the other auto wiring in a while...

Thanks for reading !!!!!!