Monday, June 18, 2012

Spring Ioc Types


Spring Ioc Types

Spring Ioc Types

Generally we have two types of IOC styles.
               
               1.Dependency lookup
               2.Dependency  injection

1.Dependency Lookup:
With dependency lookup your component is responsible to get the dependency component.
Dependency lookup
We have two types of lookup dependency mechanisms they are:
1.1. Dependency pull (DP).
1.2. Contextualized Dependency Lookup (CDL)

1.1. Dependency Pull:
In Dependency pull component will get the other dependent from a centralized registry like JNDI.

1.2 Contextualized Dependency Lookup:
Contextualized dependency lookup is similar to dependency pull, but objects will be taken from container instead of taking from centralized registry

2. Dependency Injection:
With dependency injection spring IOC container automatically injects the dependencies into the components.
Dependency Injection
 We can implement the dependency IOC in three ways.


            1. Constructor-Injection
            2. Setter-Injection
            3. Getter-Injection

1. Constructor-Injection:
In this spring container injects the dependencies through constructor.
public class B
{
        --------
        --------
        --------
}
 
public class C
{
        --------
        --------
        --------
}
 
public class A
{
        private C cobj;
        private B bobj;
        
        public A(C cobj, B bobj)
        {
               this.bobj=bobj;
               this.cobj=cobj;
        }
}


context.xml

<beans>
<bean id="cobj" class="com.javayom.C"/>
<bean id="bobj" class="com.javayom.B"/>
<bean id="aobj" class="com.javayom.A">
<constructor-arg index="1">
<ref bean="bobj" />
</constructor-arg>
<constructor-arg index="0">
<ref bean="cobj" />
</constructor-arg>
</bean>
</beans>

1. Setter-Injection:
In this bean dependencies will be injected into the component through java bean style setter method.
public class B
{
        --------
        --------
        --------
}
 
public class C
{
        --------
        --------
        --------
}
 
public class A
{
        private C cobj;
        private B bobj;
        
        public void setCobj(C cobj)
        {
               this.cobj=cobj;
        }
        public void setBobj (B bobj)
        {
               this.bobj=bobj;
        }
 
}


context.xml
 
<beans>
<bean id="cobj" class="com.javayom.C"/>
<bean id="bobj" class="com.javayom.B"/>
<bean id="aobj" class="com.javayom.A">
<property name="cobj">
<ref bean="cobj"/>
</property>
<property name="bobj">
<ref bean="bobj"/>
</property>
</bean>
</beans>

3. Getter-Injection:

public class B
{
        --------
        --------
        --------
}
 
public class C
{
        --------
        --------
        --------
}
 
 
abstract public class A
{
        abstract public B getBobj ();
}


context.xml
     
<beans>
<bean id="cobj" class="com.javayom.C"/>
<bean id="bobj" class="com.javayom.B"/>
<bean id="aobj" class="com.javayom.A">
<lookup-method name="getBobj" bean="bobj"/>
</bean>
</beans> 

No comments:

Post a Comment