9/14/10

Spring Dependency Injection

Hi folks, i have started learning about spring. So i started with dependency injection. There are numerous example in the net. i have followed few and able to come successfully . i will share the example program with you. Please take the necessary jars from spring site.

So, what is dependency injection ? Lets see an intro , then will proceed with an example.

 class A has a dependency to class B if class A uses class B as a variable.

If dependency injection is used then the class B is given to class A via
  • the constructor of the class A - this is then called construction injection
  • a setter - this is then called setter injection

The general concept between dependency injection is called Inversion of Control. A class should not configure itself but should be configured from outside.
A design based on independent classes / components increases the re-usability and possibility to test the software. For example if a class A expects a Dao (Data Access object) for receiving the data from a database you can easily create another test object which mocks the database connection and inject this object into A to test A without having an actual database connection.


Base class : 

package com.raghu.springtest;


/**
 *
 * @author raghu
 */
public class BaseClass {
public void display(String s){
    System.out.println("This class has no dependency -- "+s);
}
}

DependencyClass :(this class is dependant on class Base class)


package com.raghu.springtest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 *
 * @author raghu
 */


public class DependencyClass {


    private BaseClass bc;




    public void setbc(BaseClass bc) {
        this.bc = bc;
    }


    public void callBaseClassDisplay() {
        bc.display("Called from Dependency Class");
    }
}
 Spring xml file :

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">


    <bean id="baseclass" class="com.raghu.springtest.BaseClass"/>
    <bean id="mySpringBeanWithDependency" class="com.raghu.springtest.DependencyClass">


        <property name="bc">
            <ref bean="baseclass"/>
        </property>
    </bean>
</beans>




Main class :

package springtest;


import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.raghu.springtest.*;
/**
 *
 * @author raghu
 */
public class TestDependency {
public static void main(String[] args) {


        XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringXMLFile.xml"));




      DependencyClass dc=(DependencyClass)beanFactory.getBean("mySpringBeanWithDependency");
      dc.callBaseClassDisplay();
    }
}

No comments:

Post a Comment

Popular Posts