Professional Java Development with the Spring Framework

Extending Spring's Handler Infrastructure

When using Spring Web MVC in complex projects, you might run into situations where you need to extend the handler infrastructure itself. This can be done in one of two ways: by extending the Spring Controller interface (or by subclassing one of the classes implementing it), or by writing a new HandlerAdapter. This section covers the latter option. This is really an infrastructural feature of Spring MVC that is seldom used in normal applications because of the many other extension points offered by the framework. However, it's worth covering to round out your knowledge of the frame- work's architecture.

An org.springframework.web.servlet.HandlerAdapter defines exactly how, when, and if at all controllers are executed. Two HandlerAdapters exist, which are automatically supported when using the Spring DispatcherServlet. The first is the HandlerAdapter supporting the org.springframework.web.servlet.mvc controller infrastructure (all classes implementing the Controller interface). The second is the one supporting the org.springframework.web.servlet.mvc.ThrowayControllerHandlerAdapter.

The HandlerAdapter interface has the following methods:

As mentioned previously, two handler adapters are installed by default: the ThrowAwayControllerHandlerAdapter and the SimpleControllerHandlerAdapter. Additional handler adapters can be installed to fully customize Spring's behavior if needed, by adding them to the WebApplicationContext. They are automatically picked up by Spring, just as with the LocaleResolver or MultipartResolver beans.

While we recommend using Spring's Web MVC default Controller infrastructure, there might be situations in which you need to customize the handling of all requests in your web application.

Категории