二 拦截器 (Interceptor) 的学习 拦截器可以监听程序的一个或所有方法 拦截器对方法调用流提供了细粒度控制 可以在无状态会话 bean 有状态会话 bean 和消息驱动 bean 上使用它们 拦截器可以是同一 bean 类中的方法或是一个外部类 下面介绍如何在 Session Bean 类中使用外部拦截器类 @Interceptors 注释指定一个或多个在外部类中定义的拦截器 下面拦截器 HelloInterceptor 对 HelloChinaBean 中的所有方法进行监听 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 " 我是佛山人 "; 拦截器 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"); public Object XXX(InvocationContext ctx) throws Exception XXX 代表方法名可以任意 拦截器的实现规则 : 必须使用标注 @AroundInvoke 表示其为用作拦截器的方法, 这一方法必须遵守以下格式 :public Object xxx(javax.interceptor.invocationcontext ctx)throws Exception, 其中 xxx 可以任意取名, javax.interceptor.invocationcontext 封装了客户端所调用业务方法的一些信息 必须调用 InvocationContext 对象的 proceed() 方法作为返回结果, 否则其他拦截器和业务方法不会执行. 下面是 HelloChinaBean 的本地及远程业务接口 HelloChina.java package com.foshanshop.ejb3; public interface HelloChina extends HelloChinaRemote{ HelloChinaRemote.java package com.foshanshop.ejb3; public interface HelloChinaRemote { public String SayHello(String name); public String Myname();
下面是 Session Bean 的 JSP 客户端代码 : InterceptorTest.jsp <%@ page contenttype="text/html; charset=gbk"%> <%@ page import="com.foshanshop.ejb3.hellochinaremote, javax.naming.*, java.util.properties"%> <% props.setproperty("java.naming.factory.initial", "org.jnp.interfaces.namingcontextfactory"); props.setproperty("java.naming.provider.url", "localhost:1099"); props.setproperty("java.naming.factory.url.pkgs", "org.jboss.naming"); InitialContext ctx; try { ctx = new InitialContext(props); HelloChinaRemote hellochinaremote = (HelloChinaRemote) ctx.lookup("hellochinabean/remote"); out.println(hellochinaremote.sayhello(" 东方通 ")); out.println("<br>"+ hellochinaremote.myname()); catch (NamingException e) { out.println(e.getmessage()); %> 除了可以在外部定义拦截器之外, 还可以将 Session Bean 中的一个或多个方法定义为拦截器 下面以上 面的 HelloChinaBean 为例, 介绍在 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 注释就指定了要用作拦截器的方法 http://localhost:8080/ejbtest/interceptortest.jsp 访问客户端 TONGWEB5.0 中拦截器 HelloInterceptor 和 HelloChinaBean 写法是一致的, 只是 Session Bean 的 JSP 客户端代码有改进 1 调用的 Properties 不同, JBOSS 调用方式 : props.setproperty("java.naming.factory.initial", "org.jnp.interfaces.namingcontextfactory"); props.setproperty("java.naming.provider.url", "localhost:1099"); props.setproperty("java.naming.factory.url.pkgs", "org.jboss.naming"); TWEB5.0 调用方式 : props.setproperty("java.naming.factory.initial","com.tongweb.naming.serialinitcontextfactory "); props.setproperty("java.naming.factory.url.pkgs","com.tongweb.naming"); props.setproperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.jndist atefactoryimpl"); props.setproperty("org.omg.corba.orbinitialhost","168.1.90.6"); props.setproperty("org.omg.corba.orbinitialport","5800");
2 ctx.lookup REMOTE 方式不同 : JBOSS lookup ctx.lookup("hellochinabean/remote"); TONGWEB5.0 lookup ctx.lookup(com.foshanshop.ejb3.hellochinaremote) 代码如下 : InterceptorTest.jsp <%@ page contenttype="text/html;charset=gbk"%> <%@ page import="com.foshanshop.ejb3.hellochinaremote, javax.naming.*, java.util.properties"%> <% props.setproperty("java.naming.factory.initial","com.tongweb.naming.serialinitcontextfactory" ); props.setproperty("java.naming.factory.url.pkgs","com.tongweb.naming"); props.setproperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.jndista tefactoryimpl"); props.setproperty("org.omg.corba.orbinitialhost","168.1.90.6"); props.setproperty("org.omg.corba.orbinitialport","5800"); InitialContext ctx; try { ctx = new InitialContext(props); System.out.println("=======1=============="); HelloChinaRemote hellochinaremote=(hellochinaremote) ctx.lookup("com.foshanshop.ejb3.hellochinaremote"); System.out.println("=======3=============="); out.println(hellochinaremote.sayhello(" 东方通 ")); out.println("<br>"+ hellochinaremote.myname()); catch (NamingException e) { out.println(e.getmessage()); %> 运行结果 :http://168.1.90.6:8880/interceptor/index.jsp [2010-12-30 14:12:56] [INFO] [system.out] [=======1==============] [2010-12-30 14:12:56] [INFO] [system.out] [=======3==============] 东方通说 : 你好! 中国. 我是佛山人 [2010-12-30 14:12:56] [INFO] [system.out] [*** HelloInterceptor intercepting] [2010-12-30 14:12:56] [INFO] [system.out] [*** SayHello 已经被调用! *** ] [2010-12-30 14:12:56] [INFO] [system.out] [ 用时 :0ms] [2010-12-30 14:12:56] [INFO] [system.out] [*** HelloInterceptor intercepting] [2010-12-30 14:12:56] [INFO] [system.out] [*** Myname 已经被调用! *** ]
[2010-12-30 14:12:56] [INFO] [system.out] [ 用时 :0ms] 在 TW5.0 上调试好的应用 Interceptor.ear.rar