struts2-ejb3-plugin :struts2对ejb3支持的plugin

openkk 12年前

该plugin提供struts的Action及Interceptor对EJB组件及Resource的无侵入式依赖注入(DI)。

安 装方法: 将 struts-ejb3-plugin.jar 考入 /WEB-INF/lib 目录中。这时使用的为plugin的默认配置,该配置为 cn/agrael/struts/plugin/ejb3/default-struts-ejb3-plugin.properties中的配置。配 置信息如下:

#ENC的默认名  ENCPath=java:comp/env/  #应用服务器的具体实现类,该类是 cn.agrael.struts.plugin.ejb3.ApplicationServer的实现类  ejbContainer=cn.agrael.struts.plugin.ejb3.JbossApplicationServer  #是否解析@Resource的标志 true 为解析,false 为不解析  isParseResource=false  #是否解析@EJB的标志 true 为解析,false 为不解析  isParseEJB=true  #ear的路径名,如果没有则为空字符串  earFileBaseName=  #为远程Bean时的JNDI路径  remote=remote  #为本地Bean时的JNDI路径  local=local

如 果要修改默认的配置,需要在 classpath 下建立为 struts-ejb3-plugin.properties 的资源文件覆盖默认的配置。

除了plugin本身的配置以外,还需要在 classpath 下创建名为 jndi.properties 的资源文件用做 jndi配置 ,plugin 中使用到的jndi查找依赖于该配置。

使用例子:

@Interceptors(Interceptor3.class)  public class EjbTestAction1 extends ActionSupport{            private static final long serialVersionUID = 4126146938063764047L;            @EJB          private TestSessionBean1Local testSessionBean1Local;                    @EJB(name="ejb/sessionBean1")          private TestSessionBean1Remote sessionBean1;                    private TestSessionBean1Remote session;                    @EJB          public void setSession(TestSessionBean1Remote session) {                  this.session = session;          }            @PostConstruct          @Interceptors({Interceptor2.class,Interceptor3.class})          public void init(){                  //...          }                    @Interceptors({Interceptor3.class,Interceptor1.class,Interceptor2.class})          public String execute() throws Exception {                  //...                  return SUCCESS;          }  }

在 Interceptor中使用方式和在Action中的使用方式相同。

需要注意的是,在Action的execute(或者自定义的名称)方法中同时使用struts2的 Interceptor和@Interceptors时,@Interceptors会在Interceptor之前开始,在Interceptor之后 结束。

目前的版本暂时不支持@PreDestroy。 现阶段只有 jboss 应用服务器的实现,在以后的版本中会陆续增加如 weblogic、glassfish 等应用服务器的实现。如果现在需要 jboss 之外的实现,可实现 cn.agrael.struts.plugin.ejb3.ApplicationServer 接口,并使用 struts-ejb3-plugin.properties 修改 ejbContainer 为实现类。

项目主页:http://www.open-open.com/lib/view/home/1330770691671