Wednesday, June 12, 2013

Spring Autowire

Spring provides an excellent feature called autowiring(means resolving the dependencies automatically by container) using spring dependency injection.  You can gain some knowledge on @Autowired annotation through my previous POST where I discribed autowiring concept  using annotation.
 In this POST I am going to explain about autowiring through xml configuration. In this post I am going to cover  below three kinds of autowiring concepts.

1.       Autowire ByName
2.       Autowire ByType
3.       Autowire ByCunstructor
4.       Autowire No
5.       Autowire using “auto-detect”.

Common package structure for all these examples below.
                                                




Example 1: AutowireByName

            Below is the my bean configuration file, here I have defined one attribute called autowire="byName"  into the my CustomerServiceImpl bean configuration. This means, intimating to the spring container do autowiring based on Name. As soon as spring container found the autwire=”byName” attribute in  any spring bean configuration, container will check the setter methods defined in that bean and then it tries to match the setter method name(excluding set) and other bean name in the configuration file, if match found it will inject that bean through setter method.  

                In my example I configured two beans in my configuration file, customerServiceImpl and customerDaoImpl. customerServiceImpl bean is depending on the customerDaoImpl bean to invoke the daomethod. So to resolve dependency I am using autowire byName.

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.1.xsd">

      <bean id="customerDaoImpl" class="com.lova.springautwire.dao.CustomerDaoImpl" />
       <bean id="customerServiceImpl" class="com.lova.springautowire.service.CustomerServiceImpl" autowire="byName"/>
</beans>

CustomerServiceImpl.java

                Here setter method name setCustomerDaoImpl() , exclude the set remaining part is CustomerDaoImpl , now spring container will look for the bean with the id “customerDaoImpl” in the configuration file with the same data type of this setter method argument, if it found it will inject that bean into the CustomerServiceImpl bean, if not found it don’t do any thing. In this case daoImpl object will be Null.

Note:  setter method Name and argument data type must be match with the bean in the configuration file, then only injection will happen.



package com.lova.springautowire.service;

import com.lova.springautwire.dao.CustomerDaoImpl;

/**
 * @author Lovababu
 */
public class CustomerServiceImpl
{
       private CustomerDaoImpl daoImpl;

       public void setCustomerDaoImpl(CustomerDaoImpl daoImpl)
       {
              this.daoImpl = daoImpl;
       }

       public void serviceMethod()
       {
              System.out.println(".Service.serviceMethod().START");
              daoImpl.daoMethod();
              System.out.println(".Service.serviceMethod().END");
       }

}

CustomerDaoImpl.java

package com.lova.springautwire.dao;

/**
 * @author Lovababu
 */
public class CustomerDaoImpl
{

       public void daoMethod()
{
              System.out.println(".Dao.daoMethod()");
       }
}
 

AutowireByNameTest.java


package com.lova.springautwire.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lova.springautowire.service.CustomerServiceImpl;

/**
 * @author Lovababu
 */
public class AutowireByNameTest
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

CustomerServiceImpl serviceImpl = (CustomerServiceImpl) context.getBean("customerServiceImpl");

serviceImpl.serviceMethod();

       }

}




Example 1: AutowireByType

Spring container do inject the bean based on data type of the bean. Assume your bean is having two setter methods both are accepting same data type object, now if you configure autwire=”byType” attribute on your bean configuration spring container inject similar data type into the both setter methods.

applicationContext.xml

Here I configured my CustomerServiceImpl bean with the attribute autowire=”byType”

<?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.1.xsd">

       <bean id="customerDaoImpl1" class="com.lova.springautwire.dao.CustomerDaoImpl" />
       <bean id="customerServiceImpl" class="com.lova.springautowire.service.CustomerServiceImpl" autowire="byType"/>
</beans>

CustomerServiceImpl.java

            In my bean I have defined two setter method, both are having same data type of argument, i.e CustomerDaoImpl, now the spring container will inject the customerDaoImpl bean into the my CustomerServiceImpl bean.

package com.lova.springautowire.service;

import com.lova.springautwire.dao.CustomerDaoImpl;

/**
 * @author Lovababu
 */
public class CustomerServiceImpl
{
       private CustomerDaoImpl daoImpl;
      
       private CustomerDaoImpl anotherDaoImpl;

       public void setDaoImpl(CustomerDaoImpl daoImpl)
       {
              this.daoImpl = daoImpl;
       }
      
       public void setAnotherDaoImpl(CustomerDaoImpl daoImpl)
       {
              this.anotherDaoImpl = daoImpl;
       }

       public void serviceMethod()
       {
              System.out.println(".Service.serviceMethod().START");
              daoImpl.daoMethod();
              anotherDaoImpl.daoMethod();
              System.out.println(".Service.serviceMethod().END");
       }

}

CustomerDaoImpl.java

            Same as my first example.

AutowireByTypeTest.java

            Same as my first example just change the name.

Thank you.. Remaining two exmaples will update later.

3 comments:

UA-41474183-1