拦截器可以监听程序的一个或所有方法.拦截器对方法调用流提供了细粒度控制.可以在无状态会话 bean,有状态会话 bean 和消息驱动 bean 上使用它们.拦截器可以是同一 bean 类中的方法或是一个外部类.
下面介绍如何在Session Bean类中使用外部拦截器类.
@Interceptors 注释指定一个或多个在外部类中定义的拦截器.上面拦截器HelloInterceptor对HelloChinaBean中
的所有方法进行监听.
@AroundInvoke 注释指定了要用作拦截器的方法.用@AroundInvoke注释指定的方法必须遵守以下格式:
public Object XXX(InvocationContext ctx) throws Exception
XXX代表方法名可以任意.
下面是HelloChinaBean的本地及远程业务接口
除了可以在外部定义拦截器之外,还可以将Session Bean中的一个或多个方法定义为拦截器.
上面只需一个@AroundInvoke 注释就指定了要用作拦截器的方法.
下面介绍如何在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 注释就指定了要用作拦截器的方法.