Spring 控制器上的 Aop 注释不起作用 [英] Aop Annotation at Spring Controllers Doesn't Work

查看:50
本文介绍了Spring 控制器上的 Aop 注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为aop做了注释.当我在任何方法而不是控制器方法中使用它时,它运行良好.但是,当我在控制器的方法中使用它时,我的控制器停止工作.它开始为映射提供 404 not found 错误.我在这里发现了一个类似的问题:Spring 3 MVC @Controller with AOP 拦截器? 但我不知道该怎么做.我在控制器上的方法是:

I have made an annotation for aop. When I use it at any method rather than controller methods it works well. However when I use it at my controller's methods my controller stops working. It starts to give 404 not found error for mappings. I found a similar question here: Spring 3 MVC @Controller with AOP interceptors? but I don' know how to do it. My method at my controller is that:

@WebAuditable // This is my annotation that works at other methods
@Override
@RequestMapping(value = "/ad", method = RequestMethod.POST, headers = "Accept=application/json")
public
@ResponseBody
Cd create(HttpServletResponse response, @RequestBody Cd cd) {
    ...
}

我的控制器实现的接口是:

My interface that my controller implements is that:

public interface BaseController<T> {

    public List<T> getAll(HttpServletResponse response);

    public T getByName(HttpServletResponse response, String id);

    public T create(HttpServletResponse response, T t);

    public T update(HttpServletResponse response, T t);

}

有什么建议吗?

附注:@SeanPatrickFloyd 说:

PS: @SeanPatrickFloyd says that:

注意当使用控制器接口时(例如用于 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

推荐答案

事情是:控制器映射是在运行时完成的,如果你使用 AOP 代理,代理对象在运行时没有注解,只有它们的接口有.我可以想到两种可能的策略来解决这个限制.

The thing is: controller mapping is done at runtime, and if you use AOP proxies, the proxy objects don't have annotations at runtime, only their interfaces do. I can think of two possible strategies to work around this limitation.

要么注释通用接口方法,要么(如果您不想建议所有控制器)为每个实现类型创建一个子接口,显式注释它们的方法.我知道这是很多重写的代码,并且与 AOP 的内容相反,但是在坚持使用基于接口的代理时,我不知道更好的方法.

Either annotate the generic interface methods, or (if you don't want to advise all controllers) create a sub-interface per implementation type, explicitly annotating their methods. I know that's a lot of rewritten code and contrary to what AOP is about, but I don't know a better way when sticking with interface based proxies.

另一种方法是使用 proxy-target-class="true" 切换到 CGLib 代理.这样代理类应该(我不确定)保留注释.

Another way would be to switch to CGLib proxies using proxy-target-class="true". That way the proxy classes should (I'm not sure about this) retain the annotations.

更新:注释您的界面应该像这样工作(如果有效)

Update: annotating your interface should work like this (if it works)

public interface BaseController<T> {

    @WebAuditable
    public List<T> getAll(HttpServletResponse response);

    @WebAuditable
    public T getByName(HttpServletResponse response, String id);

    @WebAuditable
    public T create(HttpServletResponse response, T t);

    @WebAuditable
    public T update(HttpServletResponse response, T t);

}

注释基类不起作用,因为 JDK 代理不会公开任何不受接口支持的信息.

Annotating a base class won't work, because JDK proxies don't expose any information that's not backed by interfaces.

这篇关于Spring 控制器上的 Aop 注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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