如何为注释@Controller创建拦截器

时间:2012-06-29 10:12:50

标签: spring-mvc aspectj interceptor spring2.x

我加入了一个现有项目,该项目使用2个机制作为前端与spring mvc:

  • A)控制器类扩展 org.springframework.web.servlet.mvc.SimpleFormController
  • B)控制器类使用注释@Controller。

现在我发现了一个拦截器:

@Aspect
public class RequestMonitor {

    @Autowired
    private RequestMonitorService requestMonitorService;

    @Before("execution(* org.springframework.web.servlet.mvc.Controller+.handleRequest(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse))"
            + "&& args(request,response)")
    public void storeUserAccess(HttpServletRequest request, HttpServletResponse response) {
        requestMonitorService.storeUserAccess(request);
    }
}

谁收到来自org.springframework.web.servlet.mvc.Controller(机制A)的所有请求

我如何适应拦截带注释的所有其他控制器" @ Controller":org.springframework.stereotype.Controller(机制B)

1 个答案:

答案 0 :(得分:3)

你有HandlerInterceptorAdapter课程。您可以扩展它并使用子类中的方法:

public boolean preHandle(HttpServletRequest request, 
        HttpServletResponse response, Object handler) throws Exception

public void postHandle(
        HttpServletRequest request, HttpServletResponse response, 
        Object handler, ModelAndView modelAndView)
        throws Exception

你需要注册你的拦截器,即:

<mvc:interceptors>
        <bean class="my.fully.qualified.package.RequestInterceptor" />
</mvc:interceptors>