AspectJ Pointcut on Methods with Multiple Annotations

拟墨画扇 提交于 2019-12-21 21:17:00

问题


Use load-time weaving, pure AspectJ.

We have 2 annotations @Time and @Count, and a few annotated methods.

@Time (name="myMethod1Time")
@Count (name="myMethod1Count")
public void myMethod1(){..};

@Time (name="myMethod2Time")
public void myMethod2(){..};

@Count (name="myMethod3Count")
public void myMethod3(){..};

Now I am defining my own around aspect for myMethod1 which has multiple annotations:

// multiple annotations, not working
@Around("@annotation(time) && @annotation(count))
public Object myAspect(Time time, Count count) {..}

This doesn't work. However, capture method myMethod2 works fine with a single annotation:

// single annotation, is working
@Around("@annotation(time))
public Object myAnotherAspect(Time time) {..}

I want to capture only methods with both Time and Count annotations present in their signature, and I want to use the annotation value. Anyone know how to achieve this?


回答1:


Maybe combine 2 pointcuts like:

@Around("call(@Time * *(..)) && call(@Count * *(..))");


来源:https://stackoverflow.com/questions/31995233/aspectj-pointcut-on-methods-with-multiple-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!