Community Post

Spring DI in Java

Johnny Morgan

Spring DI is a framework used by Java programmers India to avail inversion of control to their applications. In this article, experts will explain right about Spring DI framework and its uses with instances.

Introduction

In Java, when developing an application we should design and code efficiently in-order to adapt any requirement/change/enhancement that comes in future. Dependency Injection makes our code to be depends on abstraction not on any concrete implementation which makes the code

  • Low coupling
  • Reusable and more adaptable to new feature
  • Testable
  • Easy to Maintainable

Spring DI is one of the framework that provides Inversion of control (i.e. it makes use of Dependency Injection by Spring Ioc Container – The Hollywood Principle “Don’t call us, we will call you”) to our application.

There are three types of dependency injection,

  • Constructor Injection
  • Setter Injection
  • Interface Injection

Constructor Injection

Dependencies are injected thru constructor parameters

e.g.

public class AirtelService {
    public void providService() { …… }
}
class TelecomClient {
    private AirtelService service;
               public TelecomClient(AirtelService service) {
        this.service = service
               }
                public void accessService() {
                  service.provideService();
                } 
}

bean.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=“telecomClient” class=“com.aegis.TelecomClient”>
      <constructor-arg>
            <bean class=“com.aegis.AirtelService”/>
       </constructor-arg>
   </bean>
</beans>
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

class Test {
   public static void main(String aa[]) {
        BeanFactory factory=new XmlBeanFactory(new ClassPathResource(“bean.xml”));  
        factory.getBean(“telecomClient”);  
        telecomClient.accessService();  
   }
} 

Setter Injection

Dependencies are injected thru setter method

e.g.

public class AirtelService {
    public void providService() { …… }
}
class TelecomClient {
    private AirtelService service;
               public void setService(AirtelService service) {
        this.service = service
               }
                public void accessService() {
                  service.provideService();
                } 
}

bean.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=“telecomClient” class=“com.aegis.TelecomClient”>
      <property>
         <bean id=“service” class=“com.aegis.AirtelService”/>
       </property>
   </bean>
</beans>
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

class Test {
   public static void main(String aa[]) {
        BeanFactory factory=new XmlBeanFactory(new ClassPathResource(“bean.xml”));  
        factory.getBean(“telecomClient”);  
        telecomClient.accessService();  
   }
} 

Interface Injection

Dependencies are injected thru setter method or constructor but the parameters/arguments are an interface. i.e. depends on the abstraction

e.g.

public interface Service {
    void providService();
}
public class AirtelService implements Service {
    public void providService() { …… }
}
class TelecomClient {
    private Service service;
               public void setService(Service service) {
        this.service = service
               }
                public void accessService() {
                  service.provideService();
                } 
}

bean.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=“telecomClient” class=“com.aegis.TelecomClient”>
      <property>
         <bean id=“service” class=“com.aegis.AirtelService”/>
       </property>
   </bean>
</beans>
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

class Test {
   public static void main(String aa[]) {
        BeanFactory factory=new XmlBeanFactory(new ClassPathResource(“bean.xml”));  
        factory.getBean(“telecomClient”);  
        telecomClient.accessService();  
   }
} 

Bean Scope

There are five type of scopes,

Singleton – A default scope of a bean in spring DI and a singleton instance i.e. only one bean created for a spring container.

Prototype – Object created at each time whenever getBean method is called

The following scopes are applicable if it is a web application that is configured with Spring Application Context

Request – Object created basis on each request (Http Request)

Session – Object created basis on user session (Http Session)

Global Session – One object created for an entire application

e.g.

<bean id=“telecomClient” class=“com.aegis.TelecomClient” scope=”protoype”>

You can anytime ask your doubts related to Spring DI and get response from java programmers India. They have specifically shared this article story with development community to make developers learn about the basics of Spring DI

Conclusion

In-order to make our application more testable, reusable and maintainable we should always depends on abstraction and should not on concretion. So, whatever the dependency injection either setter or thru constructor, it makes our code more efficient to adapt new features if the dependencies are injected thru interfaces.

Johnny Morgan

Johnny Morgan as a technical writer at Aegis Infoways since more than 5 years. I write articles especially for Java, Python and Asp.Net. I have also got well response to write articles on CRM, Hadoop and QA.