Akshay
Akshay

Reputation: 85

Not going inside the Aspect in Aspect-Oriented-Programming

Hello I am new to Aspect Oriented Programming and trying to understand some concepts. I am writing a simple Spring Boot Application where I defined a dummyEndpoint where I am calling a method of a class. I want to apply my custom annotation to that class. My code is as follows:

Endpoint in Controller

@PostMapping("/dummy")
public ResponseEntity<String> dummyRequest() {
    ClassA obj = new ClassA();
    obj.consume();
    return ResponseEntity.status(HttpStatus.OK).build();
}

CustomAnnotation

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface custom {}

CustomAnnotationAspect

@Aspect
@Component
public class CustomAnnotationAspect {
    @Around("@annotation(custom)")
    public Object trace(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = null;
        System.out.println("-----ASPECT CALL--------");
        joinPoint.proceed();
        return null;
    }
}

ClassA.java

public class ClassA {
    @custom
    public void consume() {
        // actions
    } 
}

This code is not going into Aspect when I have a request to "/dummy". Although if I put the @custom at the endpoint level i.e. just after @PostMapping("/dummy"), it goes into aspect but in my use case I want it to be just at the method level i.e. in this case at ClassA consume method.

What I am missing here? Is there any extra config needed anywhere?

Upvotes: 0

Views: 130

Answers (0)

Related Questions