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

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

问题描述

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

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我的控制器实现的接口是:

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);

}

任何建议?

PS: @SeanPatrickFloyd说:

PS: @SeanPatrickFloyd says that:


注意使用控制器接口时(例如: AOP代理),使
确保在控制器接口
而不是实现类

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代理,则代理对象不应该t在运行时有注释,只有它们的接口。我可以想出两种可能的策略来解决这个限制。

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天全站免登陆