Home > other >  @Around Pointcut not getting invoked for custom annotation
@Around Pointcut not getting invoked for custom annotation

Time:10-02

The question seems to be duplicate but none of the answers from existing questions worked. I am creating a custom annotation as below,

@Target({ ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Translate {
}

I have created an Aspect as,

@Aspect
@Configuration
public class TranslateAspect {

    @Around("@annotation(translate)")
    public Object translate(ProceedingJoinPoint joinPoint, Translate translate) throws Throwable {
        Object result = joinPoint.proceed();
        System.out.println(result); //Other logic
        return result;
    }
}

I tried providing the complete class name with the package also. The entity is getting passed to RestController,

@Entity
@Getter
@Setter
public class Pojo {
    @Translate
    private String label;
 }

But the translate method is not getting invoked whenever the new request is served.

Highly appreciate any help around this.

CodePudding user response:

Try the following:

@Aspect
@Configuration
public class TranslateAspect {

    @Around("@annotation(Translate)")
    public Object translate(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        System.out.println(result); //Other logic
        return result;
    }
}

Mind the uppercase "T" in @Around("@annotation(Translate)").

CodePudding user response:

Try this:

@Aspect
@Configuration
public class TranslateAspect {

    @Pointcut("@annotation(com.full.packagename.to.annotation.Translate)")
    public void anyTranslatableMethod() {
    }

    @Around("anyTranslatableMethod()")
    public Object translate(ProceedingJoinPoint joinPoint) throws Throwable {
        // ...
    }
}
  • Related