Aspect-oriented programming (AOP)
Aspect-oriented programming is a concept where we add a new behavior to existing code without modifying the code itself. The AOP concept is really helpful when it comes to logging or method authentication.
There are many ways you can use AOP in Spring. Let's not get into much detail, as it will be a big topic to discuss. Here, we will discuss only the @Before
pointcut and how to use @Before
in our business logic.
AOP (@Before) with execution
The term execution in AOP means having a pointcut in the @Aspect
annotation itself, and it doesn't depend on the controller API. The alternate way is that you will have to explicitly mention the annotation in the API call. Let's talk about the explicit pointcut in the next topic:
package com.packtpub.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class TokenRequiredAspect { @Before("execution(* com.packtpub...