Java Spring AOP:使建议适用于仅属于包中某些类的PostMappings

huangapple go评论77阅读模式
英文:

Java Spring AOP:Making advice work for PostMappings that belong only to certain classes in a package

问题

I can help you with the translation of the code-related content. Here's the translated code portion:

你好,我正在尝试通过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);
}

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

src>main>java>com>example>myApp>controller>(我想要的三个类在这里: EmployeeController, StudentController, UserController)

如何使这个切入点

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

仅适用于上述3个类?

英文:

Hello i am trying to apply logging to my app by using aop.At this moment i can apply the advice on all the PostMapping methods from the application by using this pointcut.

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

    }

this is the advice

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

i don't want this.i want the advice to apply to only the PostMappings that handle User Objects and i guess this refers to the 3 controllers that handle the postmappings.In this case my package structure is this.

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

How do i make this pointcut

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

apply only to the 3 classes above?

答案1

得分: 1

关于将多个切点结合在一起,将它们添加到您现有的切点中:

@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())")
英文:

What about combining several pointcus; adding those to your existing one:

@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() {

}

Then, in your advice, you could use:

@Before("postAction() && (employeeAction() || studentAction() || userAction())")

</details>



huangapple
  • 本文由 发表于 2020年7月28日 01:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63120867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定