What is dependency injection in Spring framework?

What is dependency injection in Spring framework?

                                     Design Patterns Explained – Dependency Injection with Code Examples

1)Dependency Injection is a technique that allows us to remove dependency of one component/class on another component/class. Dependency is an object on which a class depends. Injection refers to the process of injecting a dependency (object) into the dependent class. The main idea behind the pattern is to allow a class to use an object of another class without actually bothering on how to create that object.


Dependency injection is injecting dependency at run time. It is a design pattern to promote


  • Reusability of code

  • Loosely coupled views

  • Easy maintenance and testing

 

2)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.

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

  • Constructor-based Injection: The constructor arguments are injected during instance instantiation.


  • 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.


  • Interface Injection: using mapping items to inject to specific interfaces

 

3)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 managed bean scope, lifecycle, and all AOP features for which it has been configured and coded.


4)ApplicationContext is derived from BeanFactory to provide added functionality to work in web applications. 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>


Advantages of Dependency Injection:


1) Reduce coupling

both constructor and setter dependency injection reduce coupling. as in above example coupling between


2) Flexibility

Because of DI you can replace a non performance implementation with better one later.

Thanks for reading the post,I hope that will help you.

Comments