Had*_*adi 1 java spring 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方式回答。
我已经多次回答过类似的问题,例如这里:
您的情况稍微简单一些,因为您只想提取参数而不是其注释。因此,沿着其他两个答案的思路,您将使用如下切入点:
@Before("execution(* *(.., com.hadi.student.Student, ..))")
Run Code Online (Sandbox Code Playgroud)
thisJoinPoint.getArgs()然后通过迭代并检查正确的参数类型来提取建议中的参数。这比通过 直接将方法参数绑定到建议参数要慢且丑陋args(),但对于任意位置的参数来说这是您的唯一选择,因为args(.., Student, ..)会产生“不明确的参数绑定”错误。Student这是因为如果您的方法中有多个参数,AspectJ 和 Spring AOP 都无法决定应该发生什么。他们应该选择哪一个?
这是AspectJ 中的MCVE(不需要 Spring,但工作方式相同):
助手类和驱动程序应用程序:
package de.scrum_master.app;
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
package de.scrum_master.app;
public class Application {
public void doSomething() {}
public Student createStudent(String s, Student student) {
return student;
}
public Student updateStudent(Student student, String s) {
return student;
}
public void marryStudents(Student student1, Student student2) {}
public static void main(String[] args) {
Application application = new Application();
application.doSomething();
application.createStudent("x", new Student("John Doe"));
application.updateStudent(new Student("Jane Doe"), "y");
// What happens if we have multiple Student parameters?
application.marryStudents(new Student("Jane"), new Student("John"));
}
}
Run Code Online (Sandbox Code Playgroud)
方面:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import de.scrum_master.app.Student;
@Aspect
public class MyAspect {
@Before("execution(* *(.., de.scrum_master.app.Student, ..))")
public void interceptMethodsWithStudentArgs(JoinPoint thisJoinPoint) throws Throwable {
System.out.println(thisJoinPoint);
for(Object arg : thisJoinPoint.getArgs()) {
if (!(arg instanceof Student))
continue;
Student student = (Student) arg;
System.out.println(" " + student);
}
}
}
Run Code Online (Sandbox Code Playgroud)
控制台日志:
execution(Student de.scrum_master.app.Application.createStudent(String, Student))
Student [name=John Doe]
execution(Student de.scrum_master.app.Application.updateStudent(Student, String))
Student [name=Jane Doe]
execution(void de.scrum_master.app.Application.marryStudents(Student, Student))
Student [name=Jane]
Student [name=John]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8058 次 |
| 最近记录: |