Monday, 2 June 2014

Spring MVC workflow with example

   Before studying Spring,to understand the MVC architecture and advantages. So Spring framework is also depends upon the MVC Architecture. The MVC consists of three kinds of Objects i.e Model,View and Controller. Lets start explaining one by one.
1) Model-
       It handles data processing and database works part. Model processes events sent by controller. After processing these events then it sends processed data to controller (thus, controller may reprocess it) or directly to view side.

2) View-
     View prepares an interface to show to the user. Controller or model tells view what to show to the user. Also view handles requests from user and informs controller.

3) Controller-
         Let’s say controller is like brain of the system. That is right. Because it processes every request, prepares other parts of the system like model and view. Then the system determines what to do by controller’s commands.

           Advantages of MVC Framework:
1) Reusability of code
2) Easy to maintain and enhance the project.
3) Attach multiple views to a model to provide different presentations(view/model decoupling)
4) change the way a view responds to user input without changing its visual presentation” (view/controller decoupling)
5) Parallel Development will be possible.
6) Easily devide the team according to their skill(e.g Presentation and Business logic is may developed by different persons.

Now start with Spring MVC workflow.

Step 1: First request will be received by DispatcherServlet
Step 2: DispatcherServlet will take the help of HandlerMapping and get to know the Controller class name associated with the given request
Step 3: So request transfer to the Controller, and then controller will process the request by executing appropriate methods and returns ModeAndView object (contains Model data and View name) back to the DispatcherServlet
Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual view page
Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the result

 
            Front Controller has a very important role to play in the MVC Frameworks work flow. The front controller component in a MVC framework is responsible to capture the requests landing at the application and route them into the control of the MVC Framework. In Spring 3 MVC the DispatcherServlet acts as the Front Controller and is responsible to caputure the request and route it into the frameworks control.

     Apart from capturing the requests, the DispatcherServlet is also responsible to initialize the frameworks components which are used to process the request at various stages.

         DispatcherServlet Initialization Of Framework:

         During the application start up, the dispatcher servlet will initialize the following components in the framework:
  1. MultipartResolver
  2. LocaleResolver.
  3. ThemeResolver.
  4. HandlerMappings.
  5. HandlerAdapters.
  6. HandlerExceptionResolvers.
  7. RequestToViewNameTranslators.
  8. ViewResolvers.
                      Advantages of Spring compared to other Frameworks:
  • Spring is relatively light weight container in comparison to other J2EE containers. It does not use much memory and CPU cycles for loading beans, providing services like transaction control , AOP management, JDBC interaction.
  • Spring makes it easy to develop J2EE application as it has built-in support for WEB MVC framework.
  • Spring helps creating loosely coupled applications by DI.
  • Spring has no dependency on any application servers.
  • Spring creates objects lazily which helps in developing light weight applications.
  • Spring supports aspect oriented programming to manage logging, transaction, security etc.
  • Spring makes Database interaction (operations) easier as it has support for data access techniques such as DAO, JDBC, Hibernate, IBATIS, JDO etc.
  • With the help of spring application code can be unit tested easily as Spring provides unit testing support classes.
  • Spring does not need unique deployment steps.
  • Spring configuration is done in standard XML format which easy to write and understand.

3 comments: