1

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?

helloApp
  • 449
  • 1
  • 4
  • 21

1 Answers1

1

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())")
k-wasilewski
  • 3,943
  • 4
  • 12
  • 29
  • thanks for answering, yes that would work if i wouldn't have other Controller classes in that package, but i do.I have 7 Controllers in that package and only want to apply the advice on 3 of them.Any ideea how to be more specific than that in my syntax? – helloApp Jul 27 '20 at 18:41
  • after i added the code above i get the following exception :org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpointGroupsBeanPostProcessor' defined in class path resource – helloApp Jul 27 '20 at 19:05
  • [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut controllerAction – helloApp Jul 27 '20 at 19:06
  • Hm... try destructing the logical sentence (I'm not sure AspectJ provides this syntax), eg. `@Before(postAction() && employeeAction() || postAction() && studentAction() ...` – k-wasilewski Jul 27 '20 at 19:13
  • Hei, now it works it seems i had a syntax error in my signiture, thanks alot !Could you please also upvote my post for some rep, i will upvote yours since it fixed my issue. – helloApp Jul 27 '20 at 19:23
  • 1
    Glad to help ;) – k-wasilewski Jul 27 '20 at 19:25