Wednesday, June 5, 2013

Spring Introduction


    The Spring Framework is an open source application framework and inversion of control container for the Java platform. Central to the Spring Framework is its inversion of control container, which provides a consistent means of configuring and managing Java objects using reflection. The container is responsible for managing life cycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together.
    The core aspects of Spring framework is InversionOfControl and Dependency Injection.

What is exactly inversion of control?
     Inversion of control is the principle where the control flow of a program is inverted: instead the programmer controls the flow of a program; the external sources (framework, services, and other components like container) take control of it.

Let’s take an example:
     The life cycle of a servlet is controlled by the container but not by developer in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.

  • 1. If an instance of the servlet does not exist, the web container.
          a. Loads the servlet class.
          b. Creates an instance of the servlet class.
          c. Initializes the servlet instance by calling the init method.
  • 2. Invokes the service method, passing request and response objects.
  • 3. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method.
In my above example the servlet control flow of a program is inverted; instead the programmer controls the flow of program. I hope you understood.

What is dependency injection?
     I think the point of view DI is IOC, means the dependency of an object is inverted: instead it controls its own dependencies, life cycle method or framework or someone else does it for you.
Dependency Injection is a design pattern on which dependency of object is injected by framework rather than created by Object itself.

     Lets rewind my previous example, assume you have student servlet, your servlet needs request object to get the request parameters like student details from html page, now my question is who is giving request object to your servlet? Obviously servlet container, this means your servlet is depends on request object that has been injected by the container servlet.

     Inversion-of-Control (IOC) is simply the Dependency-Injection (DI) design pattern for loose coupling between business logic implementations.

     Dependency injection is alternative for Inversion of control. Check this link Click here

Thank you.

1 comment:

UA-41474183-1