Spring Boot:如何将自定义Aspect的上下文传递给Rest Controller Class

时间:2017-08-07 20:31:33

标签: spring-boot aspectj spring-aop spring-restcontroller thread-local

我已经定义了一个自定义方面,它可以查找资源进行少量验证并快速失败。如果验证成功,我如何将资源传递给我的Rest Controller类以避免额外的数据库查找

1 个答案:

答案 0 :(得分:0)

您可以这样做:

@Aspect
@Component
public class SomeAspectClass {

    @Around("execution(* com.example.controller.yourController.yourMethod (SomeClass)) && args(someObject)")
    public Object sendDataToControllerMethod(ProceedingJoinPoint proceedingJoinPoint, SomeClass someObject) {
        //Do your validation, db related works
        // "someObject" can be filled, changed etc. here
        YourMethodReturnClass yourMethodReturnObject = (YourMethodReturnClass) proceedingJoinPoint.proceed(new Object[] {someObject});
        return yourMethodReturnObject;
    }
}