The largest Interview Solution Library on the web


« Previous | 2 | | 4 | Next »

Spring - Dependency Injection

  • Java components / classes should be as independent as possible of other Java classes.
  •   
  • This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing).
  •   
  • To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.
  •    A class A has a dependency to class B if class uses class B as a variable.   

    If dependency injection is used then the class B is given to class A via:

      
  • 1.The constructor of the class A - this is then called construction injection
  • 1.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.
  •   
  • A software design based on dependency injection is possible with standard Java.
  •   
  • Spring just simplifies the use of dependency injection by providing a standard way of providing the configuration and by managing the reference to the created objects.
  •   

    Spring - Setter Injection Example

    InjectSetter.java

    package com.jtc;
    public class InjectSetter {
    private String message = null;
    public String getMessage() {
    return message;
    }
    public void setMessage(String message) {
    this.message = message;
    }
    }

    TestInjectSetter.java

    package com.jtc;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class TestInjectSetter {
    public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext("context.xml");
    InjectSetter bean = (InjectSetter) beanfactory.getBean("welcome");
    System.out.println(bean.getMessage());
    }
    }

    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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <bean id="welcome" class="com.jtc.InjectSetter">
          <property name="message" value="Setter Injection!"/>
        
       </bean>
       
    
    
    </beans>

    Below is the code for constructor Injection
    InjectConstructor.java

    package com.jtc;
    public class InjectConstructor {
    private String message = null;
    public InjectConstructor(String message) {
    this.message = message;
    }
    public String getMessage() {
    return message;
    }
    public void setMessage(String message) {
    this.message = message;
    }

    TestInjectConstructor.java

    package com.jtc;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class TestInjectConstructor {
    public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext("context.xml");
    InjectConstructor bean = (InjectConstructor) beanfactory.getBean("welcome");
    System.out.println(bean.getMessage());
    }
    }

    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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <bean id="welcome" class="com.jtc.InjectConstructor">
          <constructor-arg
    	  
    	  value="Constructor Injection!"/>
    	  
        
       </bean>
       
    
    
    </beans>

    Add Below Libraries
    commons-logging-1.1.1
    spring-aop-4.1.6.RELEASE
    spring-aspects-4.1.6.RELEASE
    spring-beans-4.1.6.RELEASE
    spring-context-4.1.6.RELEASE
    spring-context-support-4.1.6.RELEASE
    spring-core-4.1.6.RELEASE
    spring-expression-4.1.6.RELEASE
    spring-instrument-4.1.6.RELEASE
    spring-instrument-tomcat-4.1.6.RELEASE
    spring-jdbc-4.1.6.RELEASE
    spring-jms-4.1.6.RELEASE
    spring-messaging-4.1.6.RELEASE
    spring-orm-4.1.6.RELEASE
    spring-oxm-4.1.6.RELEASE
    spring-test-4.1.6.RELEASE
    spring-tx-4.1.6.RELEASE
    spring-web-4.1.6.RELEASE
    spring-webmvc-4.1.6.RELEASE
    spring-webmvc-portlet-4.1.6.RELEASE
    spring-websocket-4.1.6.RELEASE
    OutPut
    Setter Injection!
    Constructor Injection!

    « Previous | 1 | 2 | 3 | Next »


    copyright © 2014 - all rights riserved by javatechnologycenter.com