java-Spring控制器上的Aop注释不起作用

提问

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

@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) {
    ...
}

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

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说:

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代理,则代理对象在运行时没有注释,只有它们的接口可以.我可以想到两种可能的策略来解决此限制.

要么注释通用接口方法,要么(如果您不想建议所有控制器)为每种实现类型创建一个子接口,显式地对其方法进行注释.我知道这是很多重写的代码,与AOP的含义相反,但是在使用基于接口的代理时,我不知道有什么更好的方法.

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

更新:注释您的界面应该像这样(如果可以)

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代理不会公开任何没有接口支持的信息.