What is Dependency Injection- Spring Frame Work.


The technology that spring is most identified with is Dependency Injection and Flavour of IOC(Inversion of control).

Many people when they start learning springs they might confuse at DI and IOC. So in this post i would like to cover those topics to get a clear understanding.

So What is Dependency Injection ?





Background of springs:

Spring is a light-weight IOC (Inversion of Control) Container and AOP (Aspect-oriented Programming) framework. Spring makes easier the development, testing and deployment of Java enterprise applications. 

Light weight IOC :

unlike old J2EE containers, spring containers can run on stand alone  JVM (No server dependencies exsist to run the container.)

Old J2EE containers: Servlet & JSP container runs in a web server, EJB container runs in Application Server.


Servlet, JSP and EJB containers are heavyweight containers as they depend on underlying operating system for execution. Spring container is known as lightweight because it can run at command-prompt without depending on other containers.


Why Spring container is called as IoC container?
In addition to providing standard container services, Spring container additionally does Dependency Injection also. Because of Dependency Injection (which is opposite principle to Dependency Lookup), it is called as IoC container.

⇒⇒⇒
Dependency injection injects (carries) dependencies (variable values) into our Java application (from XML file). Is it confusing? Of course a lot, it’s very simple when you do understand.For those who did not understand don't worry i will give you clear explanation.
In general, we declare variables and give values to them (say from keyboard) in a Java program and use them or utmost, one Java class variables values can be used by other Java class. But in Spring, variables are declared in Java class but values are given in a XML file.
A client program reads the values from the XML file and puts them in bean code and executes. Java class depends (this is what is dependency) for the values on a XML file. The Spring container actually injects these dependencies (variables values) when a bean object is created. The advantage of dependency injection (IoC) is the code gets cleaner and gets decoupled to a higher level as bean does not search for their dependencies (variable values) where the bean is located or to say in its code.

The variable values, from XML file, can be injected (read) into the Java class in two ways – using a constructor or setter methods. That is, two Dependency Injection styles exist.
  1. Constructor Dependency Injection: Dependencies are provided (injected) with constructors of the component (bean).
  2. Setter Dependency Injection: Dependencies are provided with setter methods. It is mostly preferred to constructor dependency injection.











































Comments

Popular posts from this blog

FC Barcelona vs Real Madrid -El Clasico Rivalry.

1NF,2NF, 3NF Normalization

Memory Management (Contiguous and Non-Contiguous ).