A custom annotation in Java is created with the @interface keyword, optionally combined with @Target and @Retention to control where it can be applied and how long it's retained at runtime.
A custom @LogExecutionTime annotation, combined with an AOP aspect that intercepts any method carrying it, lets you measure and log a method's execution time just by adding one annotation, without touching the method's body.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}
@Aspect
@Component
public class ExecutionTimeAspect {
@Around("@annotation(LogExecutionTime)")
public Object logTime(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
log.info("{} took {}ms", pjp.getSignature(), System.currentTimeMillis() - start);
return result;
}
}A concise interview answer is
I define it with @interface, set @Target to say where it can be applied, and @Retention(RUNTIME) if I need to read it reflectively, for example from an AOP aspect. That's exactly the pattern behind something like a custom @LogExecutionTime annotation paired with an aspect that logs method timing.
Last updated: 11 November 2026
Previous question: What are WebFlux and Mono in Spring Boot?
Next question: How do you resolve two ambiguous URL mappings in Spring Boot without changing the HTTP method or the URL?