注释字段的 Spring AOP 切入点表达式 [英] Spring AOP Pointcut expression for annotated field

查看:28
本文介绍了注释字段的 Spring AOP 切入点表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用特定注释捕获任何字段?我的最终目标是向该字段注入一个值,但目前我的切入点是错误的(不确定正确的语法).

Is it possible to capture any field with a specific annotation? My end goal is to inject a value into that field, but currently my pointcut is wrong (not sure of the correct syntax).

@Pointcut("execution(* *(..)) && @annotation(com.mycompany.MyAnnotation)")
private void annotatedField(){}

@Around("annotatedField()")
public Object injectValue(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {}

我相信切入点是用注释说明任何方法.我想把它改成任何字段.

I believe the pointcut is stating any Method with the annotation. and I want to change that to say any Field.

推荐答案

我认为你应该听从 Sotirios Delimanolis 的建议,并尽可能使用 Spring 自带的方法.如果那时你仍然认为它们不够用并且绝对想使用方面来实现你的想法,你可以使用 Spring 中的 AspectJ 而不是 Spring AOP,并利用了像 get() 这样的切入点的全部功能set() 以拦截对类成员(静态和非静态)的读/写操作.例如:

I think you should follow the advice of Sotirios Delimanolis and get as far as possible with Spring on-board means. If then you still think they are inadequate and absolutely want to use aspects to implement your idea, you can use AspectJ within Spring instead of Spring AOP and utilise the full power of pointcuts like get() and set() in order to intercept read/write operations on class members (both static and non-static). For example:

标记注释:

package de.scrum_master.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {}

驱动程序应用:

应用程序读取(toString())和写入(构造函数,main 方法)所有字段,其中包括带注释的 status 字段.

The application reads (toString()) and writes (constructor, main method) all fields, among them the annotated status field.

package de.scrum_master.app;

public class Application {
    private int id;
    private String name;
    @MyAnnotation private String status;

    public Application(int id, String name, String status) {
        this.id = id;
        this.name = name;
        this.status = status;
    }

    @Override
    public String toString() {
        return "Application [id=" + id + ", name=" + name + ", status=" + status + "]";
    }

    public static void main(String[] args) {
        Application application = new Application(11, "AspectJ demo", "starting");
        System.out.println(application);
        application.id = 22;
        application.name = "AspectJ field access demo";
        application.status = "running";
        System.out.println(application);
        application.status = "shutting down";
        System.out.println(application);
        application.status = "stopped";
        System.out.println(application);
    }
}

方面:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotatedFieldAspect {
    @Before("get(* *) && @annotation(de.scrum_master.app.MyAnnotation)")
    public void interceptRead(JoinPoint thisJoinPoint) {
        System.out.println(thisJoinPoint);
    }

    @Before("set(* *) && @annotation(de.scrum_master.app.MyAnnotation) && args(newValue)")
    public void interceptWrite(JoinPoint thisJoinPoint, Object newValue) {
        System.out.println(thisJoinPoint + " -> " + newValue);
    }
}

控制台日志:

set(String de.scrum_master.app.Application.status) -> starting
get(String de.scrum_master.app.Application.status)
Application [id=11, name=AspectJ demo, status=starting]
set(String de.scrum_master.app.Application.status) -> running
get(String de.scrum_master.app.Application.status)
Application [id=22, name=AspectJ field access demo, status=running]
set(String de.scrum_master.app.Application.status) -> shutting down
get(String de.scrum_master.app.Application.status)
Application [id=22, name=AspectJ field access demo, status=shutting down]
set(String de.scrum_master.app.Application.status) -> stopped
get(String de.scrum_master.app.Application.status)
Application [id=22, name=AspectJ field access demo, status=stopped]

这篇关于注释字段的 Spring AOP 切入点表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆