[英]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.