[英]Spring AOP pointcut differentiated by arguements
我在同一个类中有两个公共方法,它们的返回类型相同,只是参数不同。 我只想在其中之一上应用切入点。
这是该类的示例:
public class HahaService{
public MyObject sayHaha(User user){
//do something
}
public MyObject sayHaha(Long id, User user){
//do something
}
}
现在,我只想将切入点应用于第二个sayHaha
方法,该方法需要两个参数: Long
id和User
用户。
我目前有一个@Pointcut
@Pointcut("execution(public MyObject com.abc.service.HahaService.sayHaha(..))")
private void hahaPointCut() {
}
此切入点同时应用于两个sayHaha
方法。
有没有办法我只能在第二个做到这一点?
是的,仅将切入点表达式限制为具有特定参数类型的方法。
摆脱..
并指定参数类型
@Pointcut("execution(public MyObject com.example.HahaService.sayHaha(Long, com.example.User))")
或者,如果您实际上需要参数的值,则可以使用名称绑定来捕获它们。 例如,您的切入点将被声明为
@Pointcut("execution(public MyObject com.example.HahaService.sayHaha(..)) && args(id, user)")
private void hahaPointCut(Long id, User user) {
}
并将建议(例如@Before
声明为(重复名称)
@Before("hahaPointCut(id, user) ")
public void before(Long id, User user) {
/* execute advice */
}
现在,Spring AOP可以通过切入点中的参数与args
使用的名称之间的匹配来确定参数的类型。 然后,它与@Before
匹配,并绑定到相应的调用参数。
在将参数传递给建议的章节中介绍了此技术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.