英文:
Is it possible to have multiple pointcuts with different arguments in Spring AOP?
问题
在第一个切入点有多个参数,第二个切入点只有一个参数且第二个参数传递为null是否可能?如果不可能,最佳解决方案是什么?
请注意,代码部分不包括在内。
英文:
I have single advice method with multiple pointcuts. Is it possible to have different arguments?
@Around("execution(* com.admin.web.controller.*.*.*(javax.servlet.http.HttpServletRequest,com.admin.model.PortalRequestTransaction)) && args(request,portalRequestTransaction)"
+ " || execution(* com.admin.web.controller.BaselineImporterController.*(javax.servlet.http.HttpServletRequest,..)) && args(request)")
public Object auditAround(ProceedingJoinPoint joinPoint, HttpServletRequest request,
PortalRequestTransaction portalRequestTransaction) throws Throwable { // some code here }
For example, Is it possible to the first pointcut to have 2 arguments and for the second one to have only one argument and second one passed as null?
If this is not possible, what is the best solution?
答案1
得分: 1
使用JoinPoint.getArgs()来获取方法参数作为对象数组是实现这一目标的一种方法。这涉及到反射。
要实现这一目标的示例代码如下:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TestAspect {
@Pointcut("execution(* com.admin.web.controller.*.*.*(javax.servlet.http.HttpServletRequest,com.admin.model.PortalRequestTransaction))")
public void requestNTransaction() {}
@Pointcut("execution(* com.admin.web.controller.BaselineImporterController.*(javax.servlet.http.HttpServletRequest,..))")
public void requestAsFirstArg() {}
@Around("requestNTransaction() || requestAsFirstArg()")
public Object auditAround(ProceedingJoinPoint pjp) throws Throwable {
// 获取要通知的方法的签名
System.out.println(pjp.getSignature());
Object[] args = pjp.getArgs();
for(Object arg:args) {
// 使用反射来识别和处理参数
System.out.println(arg.getClass());
}
return pjp.proceed();
}
}
注意:
- 作为切点签名的个别方法。Spring 文档
- ProceedingJoinPoint 用于
@Around
类型的通知
英文:
One way to achieve this is using JoinPoint.getArgs() to get the method arguments as an object array. This involves reflection.
A sample code to achieve this would be as follows
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TestAspect {
@Pointcut("execution(* com.admin.web.controller.*.*.*(javax.servlet.http.HttpServletRequest,com.admin.model.PortalRequestTransaction))")
public void requestNTransaction() {}
@Pointcut("execution(* com.admin.web.controller.BaselineImporterController.*(javax.servlet.http.HttpServletRequest,..))")
public void requestAsFirstArg() {}
@Around("requestNTransaction() || requestAsFirstArg()")
public Object auditAround(ProceedingJoinPoint pjp) throws Throwable {
// get the signature of the method to be adviced
System.out.println(pjp.getSignature());
Object[] args = pjp.getArgs();
for(Object arg:args) {
// use reflection to identify and process the arguments
System.out.println(arg.getClass());
}
return pjp.proceed();
}
}
Note:
- Individual methods serving as Pointcut signature . Spring documentation
- ProceedingJoinPoint for
@Around
advice type
答案2
得分: 0
是的,您可以在您的建议中有多个切入点,并且可以使用以下通用语法来完成:
@AdviceType("execution(point-cut1) || execution(point-cut2) || execution(point-cut3)")
public void foo(){}
请注意,@AdviceType
和 point-cutX
是概念性的名称,您应该分别将它们更改为您的建议和切入点。
英文:
Yes, it is possible to have multiple point-cuts in your advice, and it's done with this generic syntax:
@AdviceType("execution(point-cut1) || execution(point-cut2) || execution(point-cut3)")
public void foo(){}
Note, that @AdviceType
and point-cutX
are conceptual names, and you should change them respectively with your advice and pointcut
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论