java - AspectJ - 将接口(interface)实例变量传递给切入点

标签 java aop aspectj

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

这是我目前所拥有的:

切入点:

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() 函数中使用它。

我该怎么做?

最佳答案

虽然我不清楚您要使用 MethodTypeFieldChangeType 类实现什么,但这里有一种方法可以访问 @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());
}

关于java - AspectJ - 将接口(interface)实例变量传递给切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311021/

相关文章:

java - 通用数组索引越界

spring - 调用 Around 方面时的 AOP 异常

java - 'nested'注释的Spring AOP切入点

java - AspectJ - 有没有办法将 'hack' 设为静态最终字段?

java - ClassNotFoundException : org. aopalliance.intercept.MethodInterceptor

java - 使用 RestTemplate 和 Jackson 反序列化对 Java 的 JSON 响应

java - 使用 POI 获取 Excel 单元格的实际单元格值,无需定义数据类型

java - 表达式预期错误

java - Spring 3.1实例化bean报错

c# - PostSharp OnMethodBoundaryAspect OnEntry 未执行