Bean Class @AroundInvoke Methods

This chapter has mostly discussed interceptor classes. @AroundInvoke methods can also exist inside EJB bean classes. When used inside a bean class, the @AroundInvoke method will be the last “interceptor” to be invoked before the actual bean method:

@Stateless
public class MySessionBean implements MySessionRemote {

   public void businessMethod( ) {
      ...
   }

   @AroundInvoke
   public Object beanClassInterceptor(InvocationContext ctx) {
      try {
         System.out.println("entering: " + ctx.getMethod( ));
         return ctx.proceed( );
      } finally {
         System.out.println("leaving: " + ctx.getMethod( ));
      }
   }
}

This is a simple example of a bean class @AroundInvoke method. For what sorts of things would you want to use it? You may want to have a dynamic implementation of your bean class, or you may have an interceptor whose logic is specific to the bean.

Get Enterprise JavaBeans 3.1, 6th Edition 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.