Thursday 17 March 2016

Session1- Spring Introdunction and dependency injection

Spring framework formally developed by Red Jonson.The first name was Interface21 but as the time passed it will get on supervision of spring.org and Pivotal company. The name implies that EJB is winter and Spring solved EJB (specially EJB2 ) problems.
if we compare Spring with EJB we can say that Spring is lightweight and has no dependency to a container but EJB has dependency and it needs any application server.

We can say another example in java world AWT is dependent on OS but SWING only depends on JDK.

One of the main features of Spring is dependency-injection. In the next session we will talk about it but at this time we should tell that what is dependency injection and what we need it?

What is dependency injection ?
In the following example we have University class :
public class University {
  private Faculty faculty;
  public Department(Faculty faculty){
       this.faculty=new Faculty();
   }
}

class Faculty{
}

Whenever we want to create University we have to create Faculty also ,because University directly referenced it and created it.

By this way University is depends on Faculty so what should we do for decoupling this two classes?
We can create University and Faculty class separately and as they need each other we can inject it.
This is called Dependency Injection . DI is a design pattern.


What is IOC (Inversion of Control)  ?
Who should create the objects ? University or Faculty class or may be somebody else ?
If we invert this responsibility to some body else (external entity) we use an architectural style that is called Inversion of control.

So IOC is architectural concern and DI is a design pattern.

What is container ?
Container is a software component that contains and holds other components .
IOC container manages object creation , life cycle , identify dependencies and inject them into each other as required.