- 论坛徽章:
- 0
|
对会话BEAN 的拦截 就有点 类似于 Spring 的 AOP 操作
下面 我们 还是通过 实际的例子 来讲解
package com.ly.bean;
import javax.ejb.Remote;
@Remote
public interface SayHello {
public void say();
}
package com.ly.impl;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import com.ly.bean.SayHello;
import com.ly.intercepter.IntercepterImpl;
@Stateless(name="say")
@Interceptors(IntercepterImpl.class)
public class SayHelloImpl implements SayHello {
public void say() {
// TODO Auto-generated method stub
System.out.println("hello...");
}
}
package com.ly.intercepter;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class IntercepterImpl{
@AroundInvoke
public Object take(InvocationContext ic) throws Exception{
System.out.println("调用"+ic.getClass().getName());
return ic.proceed();
}
}
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ly.bean.SayHello;
public class Test {
public static void main(String args[])throws Exception{
Properties pro = new Properties();
pro.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
pro.setProperty("java.naming.provider.url","localhost:1099");
pro.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");
Context context = new InitialContext(pro);
SayHello say = (SayHello)context.lookup("say/remote");
say.say();
}
}
后台打印效果是:
20:50:39,968 INFO [STDOUT] 调用org.jboss.ejb3.interceptor.InvocationContextImpl
20:50:39,968 INFO [STDOUT] hello...
注意 :在 拦截器 的方法 书写规范:
第一: 返回一个 OBJECT
第二:抛出异常
第三:调用invocationcotnext.process();方法
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93826/showart_1917238.html |
|