/** * Given a handler instance, return whether this {@code HandlerAdapter} * can support it. Typical HandlerAdapters will base the decision on the handler * type. HandlerAdapters will usually only support one handler type each. * <p>A typical implementation: * <p>{@code * return (handler instanceof MyHandler); * } * @param handler the handler object to check * @return whether this object can use the given handler */ booleansupports(Object handler);
/** * Use the given handler to handle this request. * The workflow that is required may vary widely. * @param request current HTTP request * @param response current HTTP response * @param handler the handler to use. This object must have previously been passed * to the {@code supports} method of this interface, which must have * returned {@code true}. * @return a ModelAndView object with the name of the view and the required * model data, or {@code null} if the request has been handled directly * @throws Exception in case of errors */ @Nullable ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception;
/** * Same contract as for HttpServlet's {@code getLastModified} method. * Can simply return -1 if there's no support in the handler class. * @param request current HTTP request * @param handler the handler to use * @return the lastModified value for the given handler * @deprecated as of 5.3.9 along with * {@link org.springframework.web.servlet.mvc.LastModified}. */ @Deprecated longgetLastModified(HttpServletRequest request, Object handler);
/** * Does this adapter understand this advice object? Is it valid to * invoke the {@code getInterceptors} method with an Advisor that * contains this advice as an argument? * @param advice an Advice such as a BeforeAdvice * @return whether this adapter understands the given advice object * @see #getInterceptor(org.springframework.aop.Advisor) * @see org.springframework.aop.BeforeAdvice */ booleansupportsAdvice(Advice advice);
/** * Return an AOP Alliance MethodInterceptor exposing the behavior of * the given advice to an interception-based AOP framework. * <p>Don't worry about any Pointcut contained in the Advisor; * the AOP framework will take care of checking the pointcut. * @param advisor the Advisor. The supportsAdvice() method must have * returned true on this object * @return an AOP Alliance interceptor for this Advisor. There's * no need to cache instances for efficiency, as the AOP framework * caches advice chains. */ MethodInterceptor getInterceptor(Advisor advisor);
在介绍了适配器模式的基本概念和实现方式后,文章进一步探讨了适配器模式在 Spring 框架中的应用。通过分析 org.springframework.web.servlet.HandlerAdapter 和 org.springframework.aop.framework.adapter.AdvisorAdapter 这两个类的设计和实现,展示了适配器模式在 Spring 框架中的重要作用。