Eclipse+JBoss+EJB3攔截器方法和攔截器類
一、攔截器方法
EJB3可以通過攔截器對Bean方法進(jìn)行攔截和覆蓋。這有些象AOP中的around。通過AOP的around方法,可以修改被攔截方法的返回值、參數(shù)值,甚至可以取消被攔截方法的執(zhí)行。EJB3的攔截器可以用在無狀態(tài)Session Bean、有狀態(tài)Session Bean和消息驅(qū)動(dòng)Bean(MDB)的方法中。實(shí)現(xiàn)攔截器的最簡單的方法是使用攔截器方法。也就是說,只要在當(dāng)前的Bean中使用@AroundInvoke對某個(gè)方法進(jìn)行注釋(關(guān)于攔截器的類都在javax.interceptor包中),那么這個(gè)方法就會(huì)變成攔截器方法,該攔截器方法會(huì)攔截當(dāng)前Bean中的所有方法。實(shí)現(xiàn)過程如下:
- @Stateful
- public class GreeterBean implements Greeter
- {
- @AroundInvoke
- public Object myInterceptorMethod1(InvocationContext ic) throws Exception
- {
- System.out.println("myInterceptorMethod1:" + ic.getMethod().getName());
- obj = ic.proceed();
- }
- @AroundInvoke
- public Object myInterceptorMethod2(InvocationContext ic) throws Exception
- {
- System.out.println("myInterceptorMethod2:" + ic.getMethod().getName());
- obj = ic.proceed();
- }
- @Override
- public String greet(String name)
- {
- return "hello " + name;
- }
- }
上面的Stateful Session Bean中定義了兩個(gè)攔截器方法和一個(gè)Bean方法。當(dāng)客戶端調(diào)用greet方法時(shí),EJB容器會(huì)先調(diào)用myInterceptorMethod1方法,然后會(huì)調(diào)用myInterceptorMethod2方法,最后會(huì)調(diào)用greet方法。使用攔截器方法時(shí)要注意如下幾點(diǎn):
1. 攔截器方法必須有一個(gè)返回值,返回值類型是Object。
2. 攔截器方法只能有一個(gè)參數(shù),而且該參數(shù)類型必須是javax.interceptor.InvocationContext。
3. 只有調(diào)用InvocationContext接口的proceed方法,EJB容器才會(huì)調(diào)用下一個(gè)攔截器方法或被攔截的Bean方法。
4. 由于proceed方法要求拋出一個(gè)Exception異常,因此,攔截器方法必須拋出一個(gè)Exception異常,或在攔截器方法中使用try...catch來捕捉proceed方法拋出的異常。
二、攔截器類
有一些攔截器方法會(huì)攔截器不同Bean中的方法,在這種情況下,需要將攔截器方法放在一個(gè)單獨(dú)的類中。這個(gè)類就叫攔截器類。下面是一個(gè)攔截器類的代碼:
- package service;
- import javax.interceptor.AroundInvoke;
- import javax.interceptor.InvocationContext;
- public class MyInterceptor
- {
- @AroundInvoke
- public Object interceptorMethod(InvocationContext ic) throws Exception
- {
- System.out.println("MyInterceptor:" + ic.getMethod().getName());
- return ic.proceed();
- }
- }
為了使用該攔截器類,需要在SessionBean或MDB中使用@Interceptors來指定要使用的攔截器類。代碼如下:
- @Stateful
- @Interceptors(MyInterceptor.class)
- public class GreeterBean implements Greeter
- {
- @AroundInvoke
- public Object myInterceptorMethod1(InvocationContext ic) throws Exception
- {
- System.out.println("myInterceptorMethod1:" + ic.getMethod().getName());
- obj = ic.proceed();
- }
- @AroundInvoke
- public Object myInterceptorMethod2(InvocationContext ic) throws Exception
- {
- System.out.println("myInterceptorMethod2:" + ic.getMethod().getName());
- obj = ic.proceed();
- }
- @Override
- public String greet(String name)
- {
- return "hello " + name;
- }
- }
如果有多個(gè)攔截器類,可以使用如下的代碼來指定這些攔截器類:
- @Interceptors({MyInterceptor.class, MyInterceptor1.class})
如果指定了多個(gè)攔截器類和攔截器方法,就涉及到一個(gè)調(diào)用順序的問題。EJB容器會(huì)先調(diào)用攔截器類中的攔截器方法、如果有多個(gè)攔截器類被指定,按指定的順序進(jìn)行調(diào)用。也就是說,MyInterceptor類中的攔截器方法會(huì)最先被調(diào)用,然后是MyInterceptor1類中的攔截器方法。最后會(huì)調(diào)用在Bean中定義的攔截器方法(myInterceptorMethod1和myInterceptorMethod2)。
在默認(rèn)情況下,攔截器類將攔截所有的Bean方法,但可以使用@ExcludeClassInterceptors注釋來阻止攔截器對某個(gè)Bean方法進(jìn)行攔截。如在GreeterBean類中還有一個(gè)getValue方法,那么阻止該方法被攔截的代碼如下:
- @ExcludeClassInterceptors
- public String getValue()
- {
- return "abcd";
- }
使用@ExcludeClassInterceptors只能阻止攔截器類中的攔截器方法對Bean方法的攔截,而在Bean中定義的攔截器方法仍然會(huì)攔截Bean方法。
【編輯推薦】
- Eclipse+JBoss+EJB3通過繼承實(shí)體Bean將單個(gè)表映射成多個(gè)表
- Eclipse+JBoss+EJB3消息驅(qū)動(dòng)Bean
- Eclipse+JBoss+EJB3在Servlet中訪問EntityManager對象
- Eclipse+JBoss+EJB3使用命名查詢執(zhí)行JPQL
- Eclipse+JBoss+EJB3實(shí)體Bean的連接策略