我正在为我的Controller,Service和Dao层进行审核。我分别为Controller,Service和Dao提供了三个Around方面功能。我使用自定义注释,如果Controller方法上存在,将调用Around方面函数。在注释中,我设置了一个属性,我希望从Controller Around函数传递给Aspect类中的Service around函数。
public @interface Audit{
String getType();
}
我将从接口设置此getType的值。
@Around("execution(* com.abc.controller..*.*(..)) && @annotation(audit)")
public Object controllerAround(ProceedingJoinPoint pjp, Audit audit){
//read value from getType property of Audit annotation and pass it to service around function
}
@Around("execution(* com.abc.service..*.*(..))")
public Object serviceAround(ProceedingJoinPoint pjp){
// receive the getType property from Audit annotation and execute business logic
}
如何在两个Around函数之间传递对象?
答案 0 :(得分:4)
默认情况下,方面是单例对象。但是,有不同的实例化模型,这些模型在像您这样的用例中很有用。使用percflow(pointcut)
实例化模型,您可以在控制器中围绕建议存储注释的值,并在您的服务中检索建议。以下只是一个示例:
@Aspect("percflow(controllerPointcut())")
public class Aspect39653654 {
private Audit currentAuditValue;
@Pointcut("execution(* com.abc.controller..*.*(..))")
private void controllerPointcut() {}
@Around("controllerPointcut() && @annotation(audit)")
public Object controllerAround(ProceedingJoinPoint pjp, Audit audit) throws Throwable {
Audit previousAuditValue = this.currentAuditValue;
this.currentAuditValue = audit;
try {
return pjp.proceed();
} finally {
this.currentAuditValue = previousAuditValue;
}
}
@Around("execution(* com.abc.service..*.*(..))")
public Object serviceAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("current audit value=" + currentAuditValue);
return pjp.proceed();
}
}