Acting as an extention of the core Spring IoC container, this module provides robust encapsulation around standard Java Servlets to facilitate web layer development under the MVC design pattern. The system operates with a dual-container hierarchy: the root Spring application context functions as the parent, while the dedicated SpringMVC context acts as the child.
Initialization Phase
Bootstrapping initiates with the creation of the root Web Application Context via ContextLoader. Subsequently, the specific servlet bean setup occurs through FrameworkServlet. This sequence establishes the nine core strategy components defined within DispatcherServlet.properties, ensuring HandlerMapping, HandlerAdapter, and ViewResolver are instantiated based on configuration or default detection strategies.
// Configuration logic for initializing MVC strategies
protected void setupStrategies(ApplicationContext appCtx) {
// Detect all beans of type HandlerMapping or use specific bean name
this.handlerMappingsList = initHandlerMappings(appCtx);
// Locate matching adaptors for registered handlers
this.handlerAdaptersList = initHandlerAdapters(appCtx);
// Resolve view templates from logical names
initViewResolvers(appCtx);
}
Request Lifecycle Execution
Standard Servlet lifecycle methods drive the request handling. Upon invocation, service() delegates locale and attribute preparation before routing to doService(). The core distribution logic resides in doDispatch(), which orchestrates the interaction between interceptors, controllers, and views.
protected ModelAndView executeDispatch(HttpServletRequest req, HttpServletResponse res) throws Exception {
// Retrieve execution chain including interceptors
HandlerExecutionChain targetChain = locateHandler(req);
// Obtain appropriate adapter for the specific handler type
InvocableHandlerAdapter adapter = obtainAdapter(targetChain.getHandler());
// Pre-handle phase for interceptor execution
if (!targetChain.runPreInterceptors(req, res)) {
return null;
}
// Execute controller logic and produce model data
ModelAndView result = adapter.invoke(req, res, targetChain.getHandler());
// Post-handle phase
targetChain.runPostInterceptors(req, res, result);
// Render response
return renderResponse(req, res, targetChain, result, null);
}
During locateHandler, the system traverses registered mappings to find the optimal match for the incoming URL. For annotation-driven controllers, RequestMappingHandlerMapping scans candidate beans, processes annotations, and registers method-to-path mappings during the initialization phase.
Hendler Implementation Variants
Controllers can adopt different structural approaches depending on complexity requirements:
-
Direct Request Handling: Implementing
HttpRequestHandlerdirectly manages input/output streams manually.public class RawDataHandler implements HttpRequestHandler { @Override public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException { // Custom stream processing logic res.setContentType("text/plain"); } } -
Legacy Controller Interface: Extending
Controllerreturns aModelAndViewbut lacks modern parameter binding features.public class LegacyAction implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { return new ModelAndView("success_page"); } } -
Annotation-Based Approach: Utilizing
@Controllerallows for granular method mapping via@RequestMapping.@Controller public class ModernController { @GetMapping("/api/data") public String getData() { return "data_view"; } }
Resolution and Rendering
After controller logic completes, the ModelAndView is passed to the view resolution stage. The ViewResolver translates the logical view name into an actual view object. Data from the model merges with request attributes to form a combined state, wich is then dispatched via RequestDispatcher for final rendering to the client browser. Interceptor completion callbacks fire after the response generation is finalized.