【发布时间】:2016-02-28 07:55:38
【问题描述】:
自定义注释
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
}
自定义注释处理程序
@Aspect
public class TestAspectHandler {
@Around("execution(@com.test.project.annotaion.CustomAnnotation * *(..)) && @annotation(customAnnotation)")
public Object testAnnotation(ProceedingJoinPoint joinPoint, CustomAnnotation customAnnotation) throws Throwable {
System.out.println("TEST");
return result;
}
}
超类
public class AbstractDAO {
@CustomAnnotation
protected int selectOne(Object params){
// .... something
}
}
子类
public class SubDAO extends AbstractDAO {
public int selectSub(Object params) {
return selectOne(params);
}
}
子类 SubDAO 调用 SuperClass 方法 selectOne 但在 TestAspectHandler.class 中不调用 testAnnotation(...)
当我将@CustomAnnotation 移动到子类selectSub(..) 方法AspectHandler 可以得到joinPoint
如何在超类保护方法中使用自定义注解?
added
更改TestAspectHandler.testAnnotation(...) method
@Around("execution(* *(..)) && @annotation(customAnnotation)")
public Object testAnnotation(ProceedingJoinPoint joinPoint, CustomAnnotation customAnnotation) throws Throwable {
System.out.println("TEST");
return result;
}
还是不行
所以我在代码下更改我的SubDAO
public class SubDAO extends AbstractDAO {
@Autowired
private AbstractDAO abstractDAO;
public int selectSub(Object params) {
return abstractDAO.selectOne(params);
}
}
这不是完美的解决方案,但它确实有效
- 案例1:从子类方法调用超类方法不起作用
- 案例 2:创建 Super 类实例并从实例调用
【问题讨论】:
-
如果您已经阅读了我的回答,请同时阅读编辑/更新。现在有新信息。
-
Spring AOP 使用代理工作,只有对对象的方法调用通过代理,您正在执行内部方法调用,因此它不通过代理,因此不应用 AOP .除此之外,即使它会应用实际方法也不会被调用,因为你的周围方面会破坏它。
标签: java spring aspectj spring-aop