How does dependency injection work in Spring?

Spring makes application loosely coupled using Dependency Injection. Dependency Injection is an implementation of the Inversion of Control principle. DI is the process of providing the dependencies.

The core of the Spring Framework is Ioc container. The IoC container manages java objects from instantiation to destruction  through its BeanFactory. And the IoC container manages bean scope, lifecycle, and all AOP features for which it has been configured and coded.

Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes, or the Service Locator pattern…

Code is cleaner with the DI principle and decoupling is more effective when objects are provided with their dependencies. The object does not look up its dependencies, and does not know the location or class of the dependencies. As such, your classes become easier to test, in particular when the dependencies are on interfaces or abstract base classes, which allow for stub or mock implementations to be used in unit tests.

There are three types of Dependency Injection employed by the IoC container.
 *
1. Constructor-based Injection: The constructor arguments are injected during instance instantiation.
2. Setter Injection: This is the most favored method of dependency injection in Spring. Dependencies are “set” in the objects through setter methods defined in a Spring XML-based configuration file or annotation.
3. Interface Injection: using mapping items to inject to specific interfaces.

The BeanFactory is the actual container which instantiates, configures, and manages a number of beans. A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, for which there are multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory.

ApplicationContext is derived from BeanFactory to provide added functionality to work in web application. As the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended that it be used in preference to the BeanFactory, except for a few limited situations such as in an Applet, where memory consumption might be critical and a few extra kilobytes might make a difference. 

You can instantiate ApplicationContext using FileSystemXmlApplicationContext, ClassPathXmlApplicationContext , or WebXmlApplicationContext.

Here’re an example code snippet using ClassPathXmlApplicationContext:

> ApplicationContext context = new ClassPathXmlApplicationContext("/info/java/tips/config/spring-bean.xml");
HelloBean hello = (HelloBean) context.getBean("hBean");
System.out.println(hello.getMessage() + hello.getName());

With the XML-base spring configuration file:

> <bean id="refBean" class="java.lang.String">
<constructor-arg value="Mike" />
</bean>

<bean id="hBean" class="info.java.tips.bean.HelloBean">
<constructor-arg value="This is the message from Bean Constructor" />
<property name="message" value="Hello " />

<property name="name" ref="refBean"/>
</bean>

Answer by Hai Nguyen:

Spring makes application loosely coupled using Dependency Injection. Dependency Injection is an implementation of the Inversion of Control principle. DI is the process of providing the dependencies.

The core of the Spring Framework is Ioc container. The IoC container manages java objects from instantiation to destruction  through its BeanFactory. And the IoC container manages bean scope, lifecycle, and all AOP features for which it has been configured and coded.

Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes, or the Service Locator pattern…

Code is cleaner with the DI principle and decoupling is more effective when objects are provided with their dependencies. The object does not look up its dependencies, and does not know the location or class of the dependencies. As such, your classes become easier to test, in particular when the dependencies are on interfaces or abstract base classes, which allow for stub or mock implementations to be used in unit tests.

There are three types of Dependency Injection employed by the IoC container.

    1. Constructor-based Injection: The constructor arguments are injected during instance instantiation.
    2. Setter Injection: This is the most favored method of dependency injection in Spring. Dependencies are “set” in the objects through setter methods defined in a Spring XML-based configuration file or annotation.
    3. Interface Injection: using mapping items to inject to specific interfaces.

The BeanFactory is the actual container which instantiates, configures, and manages a number of beans. A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, for which there are multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory.

ApplicationContext is derived from BeanFactory to provide added functionality to work in web application. As the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended that it be used in preference to the BeanFactory, except for a few limited situations such as in an Applet, where memory consumption might be critical and a few extra kilobytes might make a difference. 

You can instantiate ApplicationContext using FileSystemXmlApplicationContext, ClassPathXmlApplicationContext , or WebXmlApplicationContext.

Here’re an example code snippet using ClassPathXmlApplicationContext:

ApplicationContext context = new ClassPathXmlApplicationContext("/info/java/tips/config/spring-bean.xml");
HelloBean hello = (HelloBean) context.getBean("hBean");
System.out.println(hello.getMessage() + hello.getName());

With the XML-base spring configuration file:

<bean id="refBean" class="java.lang.String">
<constructor-arg value="Mike" />
</bean>

<bean id="hBean" class="info.java.tips.bean.HelloBean">
<constructor-arg value="This is the message from Bean Constructor" />
<property name="message" value="Hello " />

<property name="name" ref="refBean"/>
</bean>

Hope this helps!

How does dependency injection work in Spring?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s