![](/img/trans.png)
[英]How to pass variable number of parameters to Spring Data/Hibernate/JPQL query
[英]Spring AOP - How to pass in a different number of parameters?
我有一個方面:
@Aspect
@Component
public class BusinessAspect {
@Around("@annotation(Business)")
public Object getCorrespondingBusiness(ProceedingJoinPoint joinPoint, Business business) throws Throwable {
//BEFORE METHOD EXECUTION
Object data = joinPoint.getArgs()[0]; // gets first argument
int businessNumber = business.value(); // gets # in annotation
BusinessObj correspondingBusiness = getBusiness614(); // will make modular later
// This is where ACTUAL METHOD will get invoke
Object result = joinPoint.proceed( new Object[] { data, correspondingBusiness} );
// AFTER METHOD EXECUTION
System.out.println(result);
return result;
}
private BusinessObj getBusiness614() {
return valid business..
}
}
這是需要訪問相應業務 object 的方法:
@Business(614)
public BusinessRule rangeFromGreaterThanRangeThrough(BusinessProfile businessProfile) {
return BusinessRule.businessRuleBuilder()
.withParameter("from", ...)
.withParameter("through", ...)
.withCrudOperationAction(...)
.withCrudOperationAction(...)
.setBusiness(correspondingBusiness) // not recognizing the parameter. compilation error?
).build();
}
本質上,我的問題是相應的業務 object 未被識別。 我知道你可以操縱和改變參數,但是你可以傳入額外的參數,還是只能改變它們? 如果我只能更改它們,我該怎么做才能調用此方法而無需傳入第二個參數? 為其中的每一個創建一個重載似乎有很多不必要的代碼。
謝謝!
編輯:
為了清楚起見,這里是注釋界面:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Business {
int value();
}
解決方案可以在Spring 文檔中找到:
建議調用中的參數綁定依賴於切入點表達式中使用的名稱與(建議和切入點)方法簽名中聲明的參數名稱的匹配。
如下所示定義您的Around
建議, business
參數將傳遞給建議方法。
@Around("execution(* *(..)) && @annotation(business)")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.