【问题标题】:AOP with Spring controller Function using customer Annotations使用客户注解的带有 Spring 控制器功能的 AOP
【发布时间】:2018-02-09 16:28:44
【问题描述】:

我有一个用例,我想拦截使用我的自定义注释进行注释的 Spring 控制器函数。让我们将我的注释称为@CustomerAnnotation。

我有一个控制器 MyController

public class MyController extends Controller {

@CustomerAnnotation
@RequestMappint("/test")
public void test() {
  // SOME CODE
}

我的 AspectJ 类:

@Aspect
@Component
public class CustomImpl implements CustomAspect {

    @Around("@annotation(CustomerAnnotation)")
    @Override
    public Object testAnnotation(ProceedingJoinPoint proceedingJoinPoint) {

        System.out.println("Inside annotaion");

        try {
            Object returnObj = proceedingJoinPoint.proceed();
            return returnObj;
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            return null;
        }
    };

}

每当我调用我的 API 时,我都会收到以下异常:

The mapped controller method class 'com.controller.MyController' is not an instance of the actual controller bean instance 'com.sun.proxy.$Proxy154'. If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.
HandlerMethod details: 

我的 CustomerAnnotation 类:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomerAnnotation {

}

有什么解决办法吗?

我尝试在我的服务功能上使用相同的逻辑,并且它在那里工作。

更新:当我添加 @EnableAspectJAutoProxy(proxyTargetClass = true) 时,它会给出以下异常

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class 

请帮忙

【问题讨论】:

  • 请出示您的@CustomerAnnotation 代码。
  • @ViníciusCarneirodeBrito 完成
  • 你在使用任何xml映射文件吗??
  • @Nithin xml 映射什么?
  • 显示您的 AspectJ 配置。

标签: java spring spring-mvc aop aspectj


【解决方案1】:

如果您使用 JavaConfig,请在配置类的类级别使用“import org.springframework.context.annotation.EnableAspectJAutoProxy”中的“@EnableAspectJAutoProxy”。

【讨论】:

  • 我遇到了同样的异常
  • 映射的控制器方法类 'com.controller.MyController' 不是实际的控制器 bean 实例 'com.sun.proxy.$Proxy154' 的实例。如果控制器需要代理(例如由于@Transactional),请使用基于类的代理。 Handler方法详情:
猜你喜欢
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多