1

您好,我正在尝试使用 aop 将日志记录应用到我的应用程序。此时,我可以使用此切入点将建议应用于应用程序中的所有 PostMapping 方法。

    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void postAction() {

    }

这是建议

 @Before("postAction()")
    public void beforePost(JoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder
                .getRequestAttributes())).getRequest();
        String name = request.getParameter("firstName");
        logger.info(name);
    }

我不希望这个。我希望建议仅适用于处理用户对象的 PostMappings,我猜这指的是处理 postmappings 的 3 个控制器。在这种情况下,我的包结构是这样的。

src>main>java>com>example>myApp>controller>(the 3 classes i want are here:EmployeeController,StudentController,UserController)

我怎么做这个切入点

  @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")

只适用于以上3类?

4

1 回答 1

1

结合几个pointcus怎么样?将这些添加到您现有的:

@Pointcut("within(com.example.myApp.controller.EmployeeController)")
public void employeeAction() {

}

@Pointcut("within(com.example.myApp.controller.StudentController)")
public void studentAction() {

}

@Pointcut("within(com.example.myApp.controller.UserController)")
public void userAction() {

}

然后,在您的建议中,您可以使用:

@Before("postAction() && (employeeAction() || studentAction() || userAction())")
于 2020-07-27T18:26:03.307 回答