Interceptors are the CDI way of adding custom handlers on top of a bean. For instance, our logging handler will be this interceptor in CDI:
@Log@Interceptor@Priority(Interceptor.Priority.APPLICATION)public class LoggingInterceptor implements Serializable { @AroundInvoke public Object invoke(final InvocationContext context) throws Exception { final Logger logger = Logger.getLogger(context.getTarget().getClass().getName()); logger.info(() -> "Calling " + context.getMethod().getName()); try { return context.proceed(); } finally { logger.info(() -> "Called " + context.getMethod().getName()); } }}
Decorators do the same job but they are applied automatically based on the interface(s) they implement and get the current implementation ...