简体   繁体   English

Spring AOP @Pointcut不触发@Before方法

[英]Spring AOP @Pointcut not triggering @Before method

I have an @Aspect and @Pointcut method annotated to fire @Before a @Controller request method, it seems to be matching (as I'm not getting any errors) but it is not firing my advice method at all. 我有一个@Aspect和@Pointcut方法,其注释为在@Controller请求方法之前触发@Before,它似乎是匹配的(因为我没有收到任何错误),但它根本没有触发我的建议方法。 I changed my pointcut for testing purposes to be as specific as possible and am not getting any binding errors during application startup. 我将测试的切入点更改为尽可能具体,并且在应用程序启动期间未遇到任何绑定错误。

Here's my controller method (the class is com.xyzMyController ): 这是我的控制器方法(该类是com.xyzMyController ):

@RequestMapping(method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public SubmissionResponse submitMethod(@Valid @RequestBody final SubmissionRequest request, HttpServletRequest httpRequest, BindingResult result)
{
    if (result.hasErrors()) { throw new BadRequestException(result); }

    //  ... do stuff ...
}

Here's the Aspect class: 这是Aspect类:

@Aspect
@Component
public class RequestValidatingAspect
{
    private static final Logger LOGGER = Logger.getLogger(RequestValidatingAspect.class);

    @Inject
    private ClientService clientService;

    @Inject
    private AccountService accountService;

    @Pointcut("execution(* com.x.y.z.MyController.submitMethod(*.SubmissionRequest,*.HttpServletRequest,*.BindingResult)) && args(request, httpRequest, result)")
    private void requestValidation(SubmissionRequest request, HttpServletRequest httpRequest, BindingResult result) {} 

    @Before("requestValidation(request,httpRequest,result)")    
    public void theAdvice(SubmissionRequest request, HttpServletRequest httpRequest, BindingResult result) throws Throwable
    {
        System.out.println("Before - The Advice");
        LOGGER.info("Entering The Advice!");

        if(result.hasErrors()){ throw new BadRequestException(result); }
        // ... do stuff ...

        LOGGER.info("Exiting - The Advice!");
        return;
    }
}

Turns out that the line @Pointcut wasn't entirely correct. 原来@Pointcut行并不完全正确。 Changing: 变更:

.. submitMethod(*.SubmissionRequest,*.HttpServletRequest,*.BindingResult) ..

to submitMethod(..) or using a fully qualified class name for each of the three objects with the same args filter allowed the advice to lock in on the different methods I wanted advised. 对三个对象中的每个对象使用相同的args过滤器来submitMethod(..)或使用完全限定的类名,则建议可以锁定我想要建议的不同方法。 I Wound up changing my approach a little bit though and created a custom annotation to directly signal which methods I wanted advised and wound up with this final pointcut: 我还是放弃了一点方法,并创建了一个自定义批注,以直接表示我想建议哪种方法,并最终切入了最后一个切入点:

@Pointcut("@annotation(com.xyzannotation.SpecificTypeOfRequestValidation) && args(request, httpRequest, result)")

如果您使用的是Spring 4,则可以全部使用@ControllerAdvice注释集中所有请求验证,从维护角度来看,这似乎更有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM