AspectJ - 将接口实例变量传递给切入点

时间:2016-10-28 18:20:10

标签: java aop aspectj

我需要将@interface实例变量值传递给切入点和方法,但无法在google上找到任何内容。

这是我到目前为止所做的:

切入点:

pointcut auditField(Object t, Object value): set(@ge.shemo.anotations.CaptureChanges * * ) && args(value) && target(t);

before (Object target, Object newValue, FieldChangeName fieldName): 
        auditField(target, newValue,fieldName) {
    FieldSignature sig = (FieldSignature) thisJoinPoint.getSignature();
    Field field = sig.getField();
    field.setAccessible(true);
    Object oldValue;
    try {
        oldValue = field.get(target);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Failed to create audit Action", e);
    }
    System.out.println("changed from " + oldValue + " to " + newValue);
}

和界面:

@Retention(RUNTIME)
@Target(value = FIELD)
public @interface CaptureChanges {
    MethodType fieldType();
}

已更新

public enum MethodType {
    NAME(FieldChangeType.STRING),
    ID(FieldChangeType.STRING),
    FIRST_NAME(FieldChangeType.STRING),
    LAST_NAME(FieldChangeType.STRING);

    private FieldChangeType type;

    private FieldChangeName(FieldChangeType type) {
        this.type = type;
    }

    public FieldChangeType getType() {
        return this.type;
    }
}
public enum FieldChangeType {
    ENUM, STRING
}

我想从@interface CaptureChanges获取'FieldChangeMethod方法'的值,并在before()函数中使用它。

我该怎么做?

1 个答案:

答案 0 :(得分:3)

虽然我不清楚您要使用FieldChangeType@CaptureChanges类尝试实现什么,但是当字段值为字段值时,可以使用以下方法来访问pointcut auditField(Object t, Object value, CaptureChanges captureChanges): set(* *) && @annotation(captureChanges) && args(value) && target(t); before (Object target, Object newValue, CaptureChanges captureChanges): auditField(target, newValue, captureChanges) { FieldSignature sig = (FieldSignature) thisJoinPoint.getSignature(); Field field = sig.getField(); field.setAccessible(true); Object oldValue; try { oldValue = field.get(target); } catch (IllegalAccessException e) { throw new RuntimeException("Failed to create audit Action", e); } System.out.println("changed from " + oldValue + " to " + newValue + ", fieldType=" + captureChanges.fieldType() + ", fieldChangeType=" + captureChanges.fieldType().getType()); } 注释的值即将改变:

MULTI
ZRANGE queue 0 {demand - 1}
ZREMRANGEBYRANK queue 0 {demand - 1}
EXEC