Thursday, June 7, 2018

Introduction to Spring MVC and Spring IOC

Spring framework was introduced and designed to build flexible andloosely coupled web applications. The Model-view-controller design pattern helps in seperating the business logic, presentation logic and navigation logic. But one of the key benefits that makes it so popular and useful is dependency injection (DI). DI is a design pattern, that separates an application’s dependencies and configuration from the code that uses those dependencies. Springimplements DI by allowing you to wire together an application from beans, using either an XML configuration file or annotations.
In Spring MVC Framework every piece of logic and functionality is highly configurable. Also Spring can be integrate with other popular Web Frameworks like StrutsWebWork a Java Server Faces.
DispatcherServlet is the core of Spring MVC that handles every request. The DispatcherServlet routes the request to a Controller class authored by the application developer. The controller class handles the request and decides which view should be displayed to the user as part of the response.
Spring MVC Application Flow
Now lets understand the Web Flow and request cycle of typical Spring MVCapplication. The whole lifecycle of an Spring MVC Application can be demonstrated from the following points.
Request is sent to server : User sends request to server by submitting form or by clicking hyperlink etc. TheRequest is initially given to web.xml. The DispatcherServlet configured in web.xml file receives the request and routes the request to DispatcherServlet .
Inside DispatcherServlet : DispatcherServlet is Spring MVC’s implementation of the Front controller pattern. The job of the DispatcherServlet is to take an incoming URL and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that’s supposed to be found at that location.
Data Layer Operations : If database operation is needed then Model class will route request to suitable DAO. All database operations should be carried out in DAO. Then attach attributes into request/session/application scope and return back to Model. Then the Controller executes the logic business logic (if written by the programmer) and then returns ModeAndView object to the DispatcherServlet.
Presentation Layer : The DispatcherServlet determines the view from the ModelAndView object. Some html or designing stuffs are added to response . The View is rendered and the DispatcherServlet sends the output to the Servlet container. Finally Servlet Container sends the result back to the user.
Inversion of Control
The Spring Framework is used by many frameworks. You can use the existing frameworks such as JSF, Struts 2, Flex with Spring DS to quickly develop and deploy large scale enterprise applications. The Spring framework simplifies the development of complex enterprise applications.
The Container will create the objects, wire them together, configure them, and manage their complete lifecycle from creation till destruction. The Spring container uses Dependency Injection (DI) to manage the components that make up an application. These objects are called Spring Beans.
IoC is also known as dependency injection (DI) .Inversion of control is a technique that allows object configuration to be moved out of code and into a configuration file. With Spring IOC, this is typically done with an XML file. In technical terms IoC is a Software Design Pattern and set of associated programming techniques in which the flow of control of a system is inverted in comparison to the traditional interaction mode. In IoC, instead of an application calling the framework, it is the framework that calls the components specified by the application. This pattern is similar to the one that agents adopt with their clients in Hollywood movies . As they sail ‘Don’t call me, I w’ll call you’ .This is why IoC is also known as the Hollywood Pattern.
There are three forms of Dependency Injection.
  1. Constructor Injection
  2. Setter Injection
  3. Interface Injection
Constructor Injection: In this approach , an IoC container uses the constructor to inject the dependency. All the dependencies (simple or references) are declared in the constructor. One of the advantages of Constructor Injectionis that all the dependencies are declared in one step. This also helps in understanding whether the class depends on too many services.
Setter Injection: In this approach Dependency Injection uses Setters to inject the required resources or dependencies. Each of the objects that the class depends upon will have a setter and the IoC container will use the setters to provide the resource at run-time.
Interface Injection: It is the concrete implementations of an interface to the dependent object according to the configuration. The main difference between Interface Injection and the previous two is that in Interface Injection, any of the implementations of the interface can be injected, whereas with the other two, the object of the class specified is injected. Spring does not provide direct support for Interface Injection.
Here we are done with a brief Introduction of Spring MVC and Spring IOC. In upcoming blogs we w’ll learn How to use Spring MVC to develop flexible applications.
Read more info at — www.oodlestechnologies.com

No comments:

Post a Comment