Vikas
Vikas

Reputation: 7165

@Around Pointcut not getting invoked for custom annotation

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.

Upvotes: 1

Views: 1279

Answers (3)

R.G
R.G

Reputation: 7121

From the reference documentation : 5.2. Spring AOP Capabilities and Goals

Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans). Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.

Spring AOP works with spring container managed beans and advicing method executions. The code shared here annotates a field and not the corresponding setter method. PCD defined is for the execution any Spring container managed bean method annotated with @Translate.

A class annotated with @Entity will not register its instance as a Spring managed bean. Do go through this StackOverflow Q&A

Pojo instance is not a Spring managed bean ( also pointed out by João Dias in his answer ).

Upvotes: 1

João Dias
João Dias

Reputation: 17460

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)").


UPDATE

Just noticed you are expecting the aspect to be applied to a class annotated with @Entity. These are Entities that are JPA entities but they are not Spring-managed Beans. Spring AOP only handles Spring-managed Beans so this is simply not possible.

Upvotes: 1

asgarov1
asgarov1

Reputation: 4098

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 {
        // ...
    }
}

A working example here: https://github.com/asgarov1/springTricks/blob/main/aop/src/main/java/com/asgarov/aop/config/LoggingConfiguration.java

Upvotes: 1

Related Questions