Session EJB Series (v) Interceptor

Source: Internet
Author: User

the previous article, " Session EJB Series (iv) Sessionbean transaction management , we introduce the transaction management mechanism of Sessionbean.

This topic, "interceptors," details its purpose, its role, its comparison with the spring Framework AOP, and introduces its preliminary usage.

 

I. Introduction

Enterprise-class applications often face transaction management, security checks, caching, object pool management, and so the spring Framework provides an AOP approach, flexible control of business methods! And EJB3 does not provide AOP support, then how is he coping?

crosscutting Services provided in the Java EE specification: Transaction management and security checks.

EJB Container implementation Java EE specification

Solving transaction management problems with CMT

use Jaas to resolve security checks in a declarative manner, with permission control.

for other services with crosscutting properties, the Java EE specification still seems inadequate.

to compensate for the lack of AOP support, EJB3 provides interceptor support.

The essence is the lightweight AOP implementation, which is used as an AOP

The common logic in multiple business methods is extracted from the business method, implemented in interception, and implemented in code reuse.

Two. EJB3 's Interceptor vs. Spring AOP

(a) different points

Spring's AOP Reality provides @before, @AfterReturning, @AfterThrowing, @After, @Around and many more annotation, Used to define feature rich enhancement processing.

The interceptor mechanism provided by EJBS can only be considered as a lightweight AOP implementation with limited functionality.

(b) The same point

all use @aroundinvoke to modify the interception method, defined in the method is distributed in multiple modules, with a crosscutting nature of the general processing code.

Three. How to use

(i) Define an interceptor

first, define an interceptor class and use @aroundinvoke to modify the method, which will be "cut in" when multiple methods of Sessionbean are called.

Import Javax.interceptor.aroundinvoke;import Javax.interceptor.invocationcontext;public class MyInterceptor {@ Aroundinvokepublic Object log (Invocationcontext ctx) throws Exception {long Start=system.currenttimemillis (); SYSTEM.OUT.PRINTLN ("----Interceptor run start----"); try {Object rvt = Ctx.proceed (); return rvt;} catch (Exception e) { SYSTEM.OUT.PRINTLN ("----Interceptor operation error----"); throw e;} Finally {long time = System.currenttimemillis ()-Start; SYSTEM.OUT.PRINTLN ("----Interceptor running end----"); System.out.println ("Interceptor Runtime:" + Time + "MS");}}}

as can be seen, this interceptor does not need to implement any interface, or inherit any base class, as long as the @aroundinvoke annotation tag the Interceptor class method can be.

It is important to note that the @AroundInvoke-modified method must meet the following format:

Public Object * * * * (Invocatoncontext cxt) throws Exception

The parameter Invocatoncontext object that contains the details of the called method, such as:

1.map<string,object> getcontextdata (): Returns a Map object that encapsulates the context information for this call or life cycle callback.

2.Method GetMethod (): Get intercepted methods

3.object[] getparameter (); Gets the actual parameters of the intercepted business method.

4.Object getrarget (); Gets the instance of the session bean that was intercepted

5.Object Proceed (): When calling Invocationcontext's method, it is the callback interception method that executes the intercepted method.

6.void Setparameters (object[] params): Modify the actual parameters of the intercepted business method

(ii) Definition of intercepted objects

1. Define a Sessionbean business interface and declare 3 methods

Import javax.ejb.Remote; @Remotepublic interface Helloworldbean {string Hello1 (string name); void Hello2 (); void Exclude ( );}


No need to use any annotation in the interface

just use @interceptors in the Bean implementation class to decorate it

2. Define the Bean implementation class and modify it with @Interceptors

The @Interceptors is the annotation provided by EJB3 for dependency injection , which modifies the bean class or business method, using the Value property to indicate the class name of the Interceptor class.

If a bean implementation class is decorated, it works on all methods in the class

If you decorate a method in a bean implementation class, only the tagged method works.

If you use @excludeclassinterceptors to modify a business in a bean, it means that the interceptor does not work on the markup method.

Import Javax.ejb.stateless;import Javax.interceptor.aroundinvoke;import Javax.interceptor.interceptors;import Javax.interceptor.InvocationContext; @Stateless (mappedname = "Helloworldbeanimpl") @Interceptors ( Myinterceptor.class) public class Helloworldbeanimpl implements Helloworldbean {public string hello1 (String name) { System.out.println ("I was volley by the Interceptor: Hello1");} public void Hello2 () {System.out.println ("I am the Interceptor interception method: Hello2");} public void Exclude () {System.out.println ("I am the method excluded by the Interceptor: Exclude");}}


for the use of related EJB interceptors, please refer to the implementation of interceptor in EJB3.0 "

Summarize

as you can see from the above article, the EJB3 Interceptor is a lightweight AOP mechanism that is the same as AOP in the spring framework.

and the use is extremely simple, developers only need the following steps:

(1) define a common class. How do I mark it as an interceptor class? You only need to use @aroundinvoke to decorate a method with the public Object * * * * (Invocatoncontext CXT) throws exception signature.

(2) Use @interceptors adornments on the bean implementation classes and business methods of all intercepted EJB3

(3) If you want to not use interceptors in a method in an EJB, you can use @excludeclassinterceptors to decorate the method.

Session EJB Series (v) Interceptor

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.