攔截器可以監聽程式的一個或所有方法.攔截器對方法調用流提供了細粒度控制.可以在無狀態會話 bean,有狀態會話 bean 和訊息驅動 bean 上使用它們.攔截器可以是同一 bean 類中的方法或是一個外部類.
下面介紹如何在Session Bean類中使用外部攔截器類.HelloChinaBean.java
- package com.foshanshop.ejb3.impl;
-
- import com.foshanshop.ejb3.HelloChina;
- import com.foshanshop.ejb3.HelloChinaRemote;
-
-
- import javax.ejb.Local;
- import javax.ejb.Remote;
- import javax.ejb.Stateless;
- import javax.interceptor.Interceptors;
-
- @Stateless
- @Remote ({HelloChinaRemote.class})
- @Local(HelloChina.class)
- @Interceptors({HelloInterceptor.class})
- public class HelloChinaBean implements HelloChina,HelloChinaRemote {
-
- public String SayHello(String name) {
- return name +"說:你好!中國.";
- }
-
- public String Myname() {
- return "我是佛山人";
- }
-
- }
@Interceptors 注釋指定一個或多個在外部類中定義的攔截器.上面攔截器HelloInterceptor對HelloChinaBean中
的所有方法進行監聽.
HelloInterceptor.java
- package com.foshanshop.ejb3.impl;
-
- 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 已經被調用! *** " );
- }
- if (ctx.getMethod().getName().equals("Myname")){
- System.out.println("*** Myname 已經被調用! *** " );
- }
- return ctx.proceed();
- }catch (Exception e) {
- throw e;
-
- }finally {
- long time = System.currentTimeMillis() - start;
- System.out.println("用時:"+ time + "ms");
- }
- }
- }
@AroundInvoke 注釋指定了要用作攔截器的方法.用@AroundInvoke注釋指定的方法必須遵守以下格式:
public Object XXX(InvocationContext ctx) throws Exception
XXX代表方法名可以任意.
下面是HelloChinaBean的本地及遠程業務介面HelloChinaRemote .java
- package com.foshanshop.ejb3;
-
- public interface HelloChinaRemote {
- public String SayHello(String name);
-
- public String Myname();
- }
HelloChina.java
- package com.foshanshop.ejb3;
-
- public interface HelloChina extends HelloChinaRemote{
-
- }
除了可以在外部定義攔截器之外,還可以將Session Bean中的一個或多個方法定義為攔截器.HelloChinaBean.java
- package com.foshanshop.ejb3.impl;
- import com.foshanshop.ejb3.HelloChina;
- import com.foshanshop.ejb3.HelloChinaRemote;
- import javax.ejb.Local;
- import javax.ejb.Remote;
- import javax.ejb.Stateless;
- import javax.interceptor.AroundInvoke;
- import javax.interceptor.InvocationContext;
- @Stateless
- @Remote ({HelloChinaRemote.class})
- @Local(HelloChina.class)
- public class HelloChinaBean implements HelloChina,HelloChinaRemote {
- public String SayHello(String name) {
- return name +"說:你好!中國.";
- }
- public String Myname() {
- return "我是佛山人";
- }
- @AroundInvoke
- public Object log(InvocationContext ctx) throws Exception {
- try{
- if (ctx.getMethod().getName().equals("SayHello")){
- System.out.println("*** HelloChinaBean.SayHello() 已經被調用! *** " );
- }
- if (ctx.getMethod().getName().equals("Myname")){
- System.out.println("*** HelloChinaBean.Myname() 已經被調用! *** " );
- }
- return ctx.proceed();
- }catch (Exception e) {
- throw e;
- }
- }
- }
上面只需一個@AroundInvoke 注釋就指定了要用作攔截器的方法.