[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[整理]seam Interceptor 拦截器小范例

上一篇:[转载]Seam Interceptor 拦截器
下一篇:[转]IE 无法打开Internet站点,已终止操作

添加日期:2009/4/2 10:22:45 快速返回   返回列表 阅读5336次
首先看一下EJB的定义:


import javax.ejb.Local;

@Local
public interface MyBean{
    public String hello();
}


定义了Local接口


import javax.ejb.Stateless;
import org.jboss.seam.annotations.Name;

@Stateless
@Name("myBean")
public class MyBeanImpl implements MyBean{
    public String hello(){
        return "hello world";    
    }
}


bean的实现

------------------------------
拦截器被seam当作普通的javabean来对待,即使用了@Name,也不认为它是一个对象。
@In注入是不起作用的。


@Interceptor(stateless=true,type=InterceptorType.CLIENT,within={BijectionInterceptor.class})
public class MyInterceptor extends AbstractInterceptor{
    @Aroundinvoke
    public Object aroundInvoke(InvocationContext ctx) throws Exception{
        //ctx.getMethod().getName //被调用的方法名
        //ctx.getTarget() //被调用的类
        //getComponent() //被调用的组件对象
        //ctx.proceed() //执行被调用的方法。

        //访问Session之类的东西可以通过org.jboss.seam.contexts.Contexts来查找
        (xxx) xx = (xxx)Contexts.getSessionContext.get("gaga");
        

        //JNDI来访问EJB,EJB的JNDI名可以参照components.xml中
        //<core:init... jndi-pattern="gaga/#{ejbName}/local"/>
        //也可能自己指定,用@JndiName("gagagagagagag")
        Properties prop = new Properties();
        prop.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
        prop.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
        prop.setProperty("java.naming.provider.url","127.0.0.1:1099");
        InitialContext init = new InitialContext(prop);
        MyBean bean = (MyBean)init.lookup("gaga/MyBeanImpl/local");
        System.out.println(bean.hello);

        //通过组件访问EJB
        MyBean bean2 = (MyBean)Component.getInstance("myBean");
        System.out.println(bean2.hello);

        //有错的时候,可以设置message,返回null。
        FacesContext.getCurrentInstance().addMessage("",new FacesMessage("gaga"));
        return null;
        
    }
    
}


-------------------------------------
拦截器定义完后,如何应用呢?


import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Interceptors(MyInterceptor.class)
public @interface MyChecker{}


以上是定义了一个标识。

在需要拦截的类上:

@Name("wawa")
@MyChecker
public class wawa {}

---------------------------------------
哦了。
 

评论 COMMENTS
guest649716872
2011/3/2 13:07:21
能不能,拦截到方法上,要不每次请求,所有都的方法都能拦截到
我是楼主
2011/3/3 16:17:48
呵呵,俺也不知道啊,不过,你可以判断ctx.getTarget()和ctx.getMethod().getName,它是被调用的类名和方法名,if...,只有特定的类和特定的方法名,才执行,其他的一律直接ctx.proceed();就行了。
guest859290647
2012/1/12 14:12:59
不错!

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved