如何编写适用于覆盖带注释的接口方法的方法执行的aspectj切入点?例如:
interface A {
@MyAnnotation void method();
}
class B implements A {
void method();
}
Run Code Online (Sandbox Code Playgroud)
切入点execution(@MyAnnotation * *.*(..))仅在B.method()带有注释本身时匹配.还有另一种方法吗?
我必须检测字段值的变化.我想将之前的值与新值进行比较.我不知道字段名称或类型.(更多背景信息.)对于给定类的示例:
package eu.zacheusz.aspectjtries;
@eu.zacheusz.aspectjtries.MyAnnotation
public class Sample {
private String field;
public void modify(){
this.field = "new";
}
public static void main(String[] a){
new Sample().modify();
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个方面:
package eu.zacheusz.aspectjtries.aspects;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class SampleAspect {
@After(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(value) && target(m) ")
public void afterSetField(Object m, Object value){
System.out.println("After set field. value=" + value + " target=" + m.getClass());
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个带有许多方法的服务接口,所有这些方法都接受一个Request对象并返回一个Response对象.所有请求对象都有一个共同的祖先,所有响应对象都有一个不同的共同祖先(它有一个成功标志和一个消息字段).
现在,我希望有一个检查权限等的方面,执行服务调用并返回带有失败代码的Response对象(如果有任何失败).问题是:我需要知道要创建什么类型的Response对象.是否有切入点表达式,使我可以访问返回类型?也许是这样的事情?
@Around(value = "execution(public *"
+ " com.mycompany.MyService+.*(..))"
+ " && args(request)"
+ " && returning( returnType)" // something like this would be nice
, argNames = "request,returnType")
public Object handleServiceCall(final ProceedingJoinPoint pjp,
final Request request,
final Class<? extends Response> returnType){ ... }
Run Code Online (Sandbox Code Playgroud) 我是AspectJ的初学者,所以请指导我按照以下方法解决发生的问题.
@Aspect
public class TestAop {
@Pointcut("execution(public * com.packg.foo.ClassOne.*(..))")
public void fooPoint()
@Pointcut("execution(public * com.packg.cat.ClassTwo.*(..))")
public void catPoint()
@Pointcut("execution(public * com.packg.roo.ClassThree.*(..))")
public void rooPoint()
@Around("fooPoint() || catPoint() || rooPoint()")
public Object myAdvice(ProceedingJoinPoint joinPoint) {
//do something like joint proceed and all
}
Run Code Online (Sandbox Code Playgroud)
什么时候不工作?如果我将所有三个切入点与OR组合在一起.
什么时候工作?如果我只保留两个切入点就可以了.
我违反了@around建议的任何规则.是否可以有多个执行/切入点?
希望得到答案......
这应该很简单.
问题
如何在一个项目中获得切入点以建议另一个项目中的代码/类?
目前,我正在调试这两个项目之间的交互.为了帮助解决这个问题,我正在编写一个Aspect(在科学项目中)来记录关键信息,因为数学代码(和科学代码)会执行.
package org.science.example;
public aspect ScientificLog {
public pointcut testCut() : execution (public * *.*(..));
before() : testCut() {
//do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
org.math.example横切的课程,全部!proect properties > AspectJ Build > Inpath并点击添加项目并选择数学项目.这没用,但似乎我需要按照这些方式做点什么.
在此先感谢您的任何建议...
-gMale
Caused by: org.aspectj.weaver.BCException: Unable to continue, this version of AspectJ supports classes built with weaver version 6.0 …