带有 AOP 拦截器的 Spring 3 MVC @Controller? [英] Spring 3 MVC @Controller with AOP interceptors?

查看:29
本文介绍了带有 AOP 拦截器的 Spring 3 MVC @Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么显然不可能将 AOP 与带注释的 MVC 控制器一起使用?(参见发布).我有一个@Controller,一旦我向它添加切入点,它就会停止工作.问题不在于没有调用拦截器,而是 @Controller 只是停止工作(在日志中,您可以看到映射 URL 路径 [/xx] 到处理程序 'Yyy'",而是无 URL"已识别路径").

Anyone knows why apparently it is not possible to use AOP with annotated MVC Controllers? (see Post). I have a @Controller that stops working as soon as I add a pointcut to it. The problem is not that the interceptor is not being called, but rather the @Controller simply stops working (in the log you can see that instead of "Mapped URL path [/xx] onto handler 'Yyy'" you get a "no URL paths identified").

我知道有一种机制可以通过 handlerMapping 向控制器添加拦截器,但我的问题是特定于 AOP 拦截器的.与任何其他 pojo 一样,带注释的控制器不只是 Spring 容器中的 pojos 吗?有什么区别?为什么?

I know there is a mechanism for adding interceptors to controllers via the handlerMapping but my question is specific to AOP interceptors. Aren't annotated controllers just pojos in the Spring container as any other pojo? What is the difference? Why?

@Controller
@RequestMapping("/user")
public class RestTestImpl implements RestTest {
    @RequestMapping(value="/", method={RequestMethod.GET})
    public @ResponseBody String deleteUsers(String arg) {
        return "Xxxxx";
    }
}

在我的 servlet-Context 中,我有:

In my servlet-Context I have:

<context:component-scan base-package="org.xxx.yyy"></context:component-scan>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    . . .
</bean>

一切都很好.

但是当我添加:

    <aop:config>
        <aop:pointcut expression="execution(* org.xxx.*(..))" id="pc1"/>
        <aop:advisor advice-ref="hibernateInterceptor"  pointcut-ref="pc1" order="2" />
    </aop:config>

控制器不再是控制器(没有错误,只是停止绑定到指定的 URL)!

The controller stops being a controller (no errors, simply it stops binding to the specified URL)!

推荐答案

来自 Spring MVC 参考:

注意
使用控制器接口时(例如用于 AOP 代理),确保始终把你所有的映射注释 - 例如@RequestMapping@SessionAttributes- 在控制器接口上,而不是在实现类上.

Note
When using controller interfaces (e.g. for AOP proxying), make sure to consistently put all your mapping annotations - such as @RequestMapping and @SessionAttributes - on the controller interface rather than on the implementation class.

当然,这个笔记隐藏得很好:-)

Granted, this note is well hidden :-)

这篇关于带有 AOP 拦截器的 Spring 3 MVC @Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆