Search code examples
spring-bootaopaspectj

How to get Aspect working on all getters in Pojo


I am aiming to do some updates for all getters in Pojo by using Aspect and Spring AOP. I would like to do something like I can get "haha" + Str when I do the getter.

Here is my Aspect:

@Slf4j
@Aspect
@Configuration
public class GetterAspect {
  @Before("execution(* com.docusign.docusign.dto.AspectPojo.getName())")
  public void before(JoinPoint joinPoint) {
    log.info(" Check before pojo");
    log.info("", joinPoint);
  }
}

Here is my Pojo:

@Data
public class AspectPojo {
  private String name;
  private String email;
}

Here is my test ctrl:

@GetMapping
public AspectPojo get() {
  AspectPojo pojo = aspectManager.getPojo();
  log.info(pojo.getName());
  return pojo;
}

I can't get the pointcut triggered, I'd appreciate if anyone can help out.


Solution

  • Spring AOP support not all feature of AspectJ standard. How I remember only public methods of Spring beans could be handled by Spring AOP. If your getters are in @Controller, @Component or @Service annotated class it will be handled but for example JPA entities cannot be handled this way.