News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Autowiring in Spring

Started by thiruvasagamani, Sep 24, 2008, 11:04 AM

Previous topic - Next topic

thiruvasagamani

Autowiring in Spring

In a large application, all of explicit constructor or property wiring will make XML lengthy and hard to debug if anything needs to change in between..
Instead of explicitly wiring all of
your bean's properties, you can use Spring autowire features to wire the dependencies for you. Spring provides the following four types of autowiring:-


   1. byName—Attempts to find a bean in the container whose name (or ID) is
      the same as the name of the property being wired. If a matching bean is not
      found, the property will remain unwired.

   2. byType—Attempts to find a single bean in the container whose type
      matches the type of the property being wired. If no matching bean is found,
      the property will not be wired. If more than one bean matches, an
      org.springframework.beans.factory.UnsatisfiedDependencyException

      will be thrown.

   3. constructor—Tries to match up one or more beans in the container with
      the parameters of one of the constructors of the bean being wired. In the
      event of ambiguous beans or ambiguous constructors, an org.springframework.beans.factory.UnsatisfiedDependencyException will be thrown.

   4. autodetect—Attempts to autowire by constructor first and then using
      byType. Ambiguity is handled the same way as with constructor and
      byType wiring.


Example to Demonstrate byName and byType Autowiring :-

Note:-We have created one more xml file spring-beans1.xml for the Autowiring examples.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

default-destroy-method="destroy" default-init-method="createInstance">

   
<bean id="test" class="com.visualbuilder.beans.TestBean" init-method="createInstance" destroy-method="destroy" />

   
<bean id="disposableInitBean" class="com.visualbuilder.beans.DisposableInitBean" />

   
<bean id="collectionInjectionBean" class="com.visualbuilder.beans.CollectionInjectionBean" >

        <property name="products">

            <list>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </list>

        </property>

        <property name="productSet">

            <set>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </set>

        </property>

        <property name="prices">

            <map>

                <entry key="CAR" value="100" />

                <entry key="MOTORCYCLE" value="200" />

                <entry key="SCOOTER" value="10" />

            </map>

        </property>

    </bean>

    <bean id="typeCollectionInjectionBean" class="com.visualbuilder.beans.TypeCollectionInjectionBean" >

        <property name="products">

            <list>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </list>

        </property>

        <property name="prices">

            <map>

                <entry key="CAR" value="100" />

                <entry key="MOTORCYCLE" value="200" />

                <entry key="SCOOTER" value="10" />

            </map>

        </property>

    </bean>

   
<bean id="constructorInjectionBean" class="com.visualbuilder.beans.ConstructorInjectionBean" >

       
<constructor-arg type="java.lang.String" value="John Smith" />

       
<constructor-arg index="1" value="25" />

       
<constructor-arg value="M" />

   
</bean>

    <bean id="setterInjectionBean" class="com.visualbuilder.beans.SetterInjectionBean" >

        <property name="name" value="John Smith" />

   
    <property name="age" value="25" />

        <property name="sex" value="M" />

    </bean>

    <bean id="wrapperBeanByName" class="com.visualbuilder.beans.WrapperBean" autowire="byName"></bean>

   
<bean id="wrapperBeanByType" class="com.visualbuilder.beans.WrapperBean" autowire="byType"></bean>

</beans>


AutoDetectExample.java




package com.visualbuilder.example;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.visualbuilder.beans.WrapperBean;

public class AutoDetectExample {

    public static void main(String[] ar) {

       
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "com/visualbuilder/xml/spring-beans1.xml" });

       
System.out.println("Geting the Bean Autowired byName");

       
WrapperBean bean = (WrapperBean) context.getBean("wrapperBeanByName");

    bean.getConstructorInjectionBean().printData();

       
System.out.println("Geting the Bean Autowired byType");

       
WrapperBean bean1 = (WrapperBean) context.getBean("wrapperBeanByType");

        bean1.getConstructorInjectionBean().printData();

   
}

    }


Output:-

Jun 8, 2008 10:11:33 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Jun 08 10:11:33 IST 2008]; root of context hierarchy

Jun 8, 2008 10:11:34 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans1.xml]

Jun 8, 2008 10:11:34 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9

Jun 8, 2008 10:11:34 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9: defining beans [test,disposableInitBean,collectionInjectionBean,typeCollectionInjectionBean,constructorInjectionBean,setterInjectionBean,wrapperBeanByName,wrapperBeanByType,wrapperBeanAuto,wrapperBeanConstructor]; root of factory hierarchy

afterPropertiesSet method is called

Geting the Bean Autowired byName

Assigned Name is John Smith

Assigned Age is 25

Assigned Sex is M

Geting the Bean Autowired byType

Assigned Name is John Smith

Assigned Age is 25

Assigned Sex is M
Thiruvasakamani Karnan