O'Reilly logo

Java EE 8 High Performance by Romain Manni-Bucau

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required