Author
In Spring AOP, custom annotations allow you to declaratively mark methods or classes for aspect-oriented behavior. This approach improves readability, modularity, and maintainability by separating cross-cutting concerns (like logging, security, or performance monitoring) from business logic.
User-defined annotations that mark methods or classes for AOP advice.

In Spring AOP, custom annotations mark methods or classes for aspects, enabling clean, modular, and maintainable handling of cross-cutting concerns like logging, security, and performance.
A custom annotation is a user-defined annotation used to mark methods or classes for AOP advice.
Example: Logging execution time of a method
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)// Available at runtime
@Target(ElementType.METHOD)// Can be applied to methods
public@interface LogExecutionTime { }
Explanation:
@Retention(RetentionPolicy.RUNTIME) → Annotation available at runtime for AOP.@Target(ElementType.METHOD) → Can be applied only to methods.✅ Marks methods where cross-cutting logic (e.g., logging execution time) should apply.
Once the custom annotation is created, you define an Aspect that applies advice to methods annotated with it.
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
publicclassLoggingAspect {
@Around("@annotation(com.example.annotation.LogExecutionTime)")
public ObjectlogExecutionTime(ProceedingJoinPoint joinPoint)throws Throwable {
longstart= System.currentTimeMillis();
Objectresult= joinPoint.proceed();// Execute method
longend= System.currentTimeMillis();
System.out.println(joinPoint.getSignature() +" executed in " + (end - start) +"ms");
return result;
}
}
Explanation:
@Around("@annotation(...)") → Matches methods annotated with @LogExecutionTime.joinPoint.proceed() → Executes the target method.Now you can use the custom annotation on any method in your business/service class.
import org.springframework.stereotype.Service;
@Service
publicclassPaymentService {
@LogExecutionTime
publicvoidprocessPayment() {
System.out.println("Processing payment...");
}
}
Explanation:
processPayment() is advised by the aspect.Output Example:
PaymentService.processPayment executed in 12ms
Processing payment...
✅ Only annotated methods are advised. No need for explicit pointcut expressions.
| Feature | Benefit |
|---|---|
| Readable & Declarative | Easily see which methods are advised by looking at the annotation. |
| Reusable | Apply the same annotation to multiple methods or classes. |
| Maintainable | Change advice logic only in the aspect, not in business code. |
| Clean Separation | Keeps cross-cutting concerns separate from core business logic. |
@AdminOnly)@LogExecutionTime, @AdminOnly).Custom annotations in Spring AOP allow developers to:
Ultimate Tip: Custom annotations + Spring AOP = clean, maintainable, and scalable code for enterprise applications.