相关疑难解决方法(0)

如何根据带注释的参数编写Aspect切入点

我在解决如何创建一个可以在具有特定注释参数的bean上运行的切入点时遇到了一些麻烦.我最终的目标是在处理参数之前验证参数的值,但目前我只需要创建切入点.

请考虑以下注释

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface MyAnnotation {}
Run Code Online (Sandbox Code Playgroud)

然后,我想将其应用于以下方法:

public void method1(@MyAnnotation long i) {}
public void method2(String someThing, @MyAnnotation long i) {}
public void method3(String someThing, @MyAnnotation long i, byte value) {}
Run Code Online (Sandbox Code Playgroud)

所以

  • 我不关心方法所在的类(或包)
  • 注释参数的位置会有所不同.
  • 我知道注释值只适用于特定类型

我的切入点实现需要有以下几点:

@Before(value = "* *(..) && args(verifyMe)")
public void verifyInvestigationId(long verifyMe) {}
Run Code Online (Sandbox Code Playgroud)

我对这个@Before值究竟是什么以及如何将注释及其类型联系起来感到有些困惑.在这一点上,可能不值得列出我尝试过的东西!

更新:基于我在http://stackoverflow.com/questions/3565718/pointcut-matching-methods-with-annotated-parameters/3567170#3567170中看到的建议(纠正了一些误解并增加了我忽略的空间) )我已经到了以下工作的地步:

@Before("execution(public * *(.., @full.path.to.MyAnnotation (*), ..))")
public void beforeMethod(JoinPoint joinPoint) {
    System.out.println("At least one of the parameters are annotated with @MyAnnotation"); …
Run Code Online (Sandbox Code Playgroud)

java spring annotations aspectj

7
推荐指数
1
解决办法
4132
查看次数

AspectJ:切入点中的参数

我正在使用AspectJ来建议所有具有所选类参数的公共方法.我尝试了以下方法:

pointcut permissionCheckMethods(Session sess) : 
    (execution(public * *(.., Session)) && args(*, sess));
Run Code Online (Sandbox Code Playgroud)

这对于具有至少2个参数的方法非常有用:

public void delete(Object item, Session currentSession);
Run Code Online (Sandbox Code Playgroud)

但它不适用于以下方法:

public List listAll(Session currentSession);
Run Code Online (Sandbox Code Playgroud)

我怎样才能改变我的切入点来建议两种方法执行?换句话说:我希望".."通配符代表"零个或多个参数",但看起来它意味着"一个或多个"......

aspectj

6
推荐指数
1
解决办法
2万
查看次数

Spring AOP 参数


我对 Spring AOP 有疑问。我想在切入点中获取 Student 对象。但我的 JoinPoints 可以以任何优先级拥有该对象。
查看下面的代码片段,了解我创建的两个不同的连接点和切入点:

public Student createStudent(String s, Student student) {...}
public Student updateStudent(Student student, String s) {...}

@Before("args(..,com.hadi.student.Student)") 
public void myAdvice(JoinPoint jp) {
    Student student = null;
    for (Object o : jp.getArgs()) {
        if (o instanceof Student) {
            student = (Student) o;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码仅适用于第一个 JoinPoint。所以问题是如何创建一个切入点,该切点将在输入参数中针对 Student 的任何情况执行。
我无法使用下面的代码,它抛出runtimeException:
@Before("args(..,com.hadi.student.Student,..)")

我让代码变得容易理解,实际上我的切入点比这个大得多。所以请用args方式回答。

java spring spring-aop

1
推荐指数
1
解决办法
8058
查看次数

标签 统计

aspectj ×2

java ×2

spring ×2

annotations ×1

spring-aop ×1