Interceptors/decorators

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 ...

Get Java EE 8 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.