EJB Learning Note Six (Interceptor in EJB)

Source: Internet
Author: User

1. Preface

It is not familiar to hear interceptors, especially in the servlet specification, where the concept of interceptors is fully applied. EJB3 also provides interceptor support, which is essentially a lightweight AOP implementation that allows the interceptor to pull away common logic from business methods in multiple business methods and implement it in interceptors for excellent code reuse.


interceptors in 2.EJB

The AOP implementations in spring provide a large number of annotation, such as @before, @AfterReturning, @AfterThrowing, to define feature-rich enhancement processing, but the EJB interceptor does not intend to implement full AOP , just provide a @aroundinvoke Annotation, the function is relatively limited.


3. Definition of interceptors

It is very simple to define an interceptor in an EJB, just using @aroundinvoke to decorate an interceptor method.


Interceptor class

Package com. Interceptor;import Javax.interceptor.aroundinvoke;import Javax.interceptor.invocationcontext;public Class Hellointerceptor {@AroundInvoke public    Object log (Invocationcontext ctx) throws Exception {        SYSTEM.OUT.PRINTLN ("* * * * hellointerceptor intercepting");        Long start = System.currenttimemillis ();        try{            if (Ctx.getmethod (). GetName (). Equals ("SayHello")) {                System.out.println ("* * * SayHello has been called!) *** " );            }                        if (Ctx.getmethod (). GetName (). Equals ("Myname")) {                System.out.println ("* * * Myname has been called!) *** " );            }                        return Ctx.proceed ();        } catch (Exception e) {            throw e;                    } Finally {            long time = System.currenttimemillis ()-Start;                System.out.println ("spents:" + time + "MS");}}}    

Analysis: From the above code can be seen, this interceptor does not need to implement any interface, or inherit any base class, as long as the @aroundinvokeannotation label the Interceptor class method. The Interceptor method must meet the following format:

Public Object XXX (Invocationcontext ctx) throws Exception


Modifier class

Package com. Interceptor;import Javax.ejb.remote;import Javax.ejb.stateless;import javax.interceptor.ExcludeClassInterceptors; Import javax.interceptor.Interceptors; @Stateless @remote (Hellochinaremote.class) @Interceptors (        Hellointerceptor.class) public class Hellochinabean implements Hellochinaremote {public string SayHello (String name) {    return name + "Hello";    }//This note, when intercepted, can be excluded from this method @ExcludeClassInterceptors public String Myname () {return ' my name '; }//write directly here also can/* @AroundInvoke public Object log (Invocationcontext ctx) throws Exception {SYSTEM.O               UT.PRINTLN ("* * * * hellointerceptor intercepting");        Long start = System.currenttimemillis (); try{if (Ctx.getmethod (). GetName (). Equals ("SayHello")) {System.out.println ("* * * SayHello has been called!)            *** " ); } if (Ctx.getmethod (). GetName (). Equals ("Myname")) {System.out.println ("* * * Myname has been Call!      *** " );      } return Ctx.proceed ();                    }catch (Exception e) {throw e;                }finally {Long time = System.currenttimemillis ()-Start;        System.out.println ("spents:" + time + "MS"); }    }*/}


4. Summary

1. Define an interceptor class without any special features, as long as you use @aroundinvoke to decorate a method with public Object XXX (Invocationcontext ctx) throws exception signature

2. Use @interceptors adornments on all bean implementation classes and business methods of EJB3 that need to be intercepted

3. If you want to exclude a business method in the EJB from being intercepted, use @excludeclassinterceptors to decorate the method.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

EJB Learning Note Six (Interceptor in EJB)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.