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

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

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方法。

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

这是建议部分。

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

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

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

如何使这个切入点

  1. @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.

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

this is the advice

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

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.

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

How do i make this pointcut

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

apply only to the 3 classes above?

答案1

得分: 1

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

  1. @Pointcut("within(com.example.myApp.controller.EmployeeController)")
  2. public void employeeAction() {
  3. }
  4. @Pointcut("within(com.example.myApp.controller.StudentController)")
  5. public void studentAction() {
  6. }
  7. @Pointcut("within(com.example.myApp.controller.UserController)")
  8. public void userAction() {
  9. }

然后,在您的通知中,您可以使用:

  1. @Before("postAction() && (employeeAction() || studentAction() || userAction())")
英文:

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

  1. @Pointcut("within(com.example.myApp.controller.EmployeeController)")
  2. public void employeeAction() {
  3. }
  4. @Pointcut("within(com.example.myApp.controller.StudentController)")
  5. public void studentAction() {
  6. }
  7. @Pointcut("within(com.example.myApp.controller.UserController)")
  8. public void userAction() {
  9. }

Then, in your advice, you could use:

  1. @Before("postAction() && (employeeAction() || studentAction() || userAction())")
  2. </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:

确定