Struts2 EJB3 Plugin
該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/
#應用服務器的具體實現(xiàn)類,該類是 cn.agrael.struts.plugin.ejb3.ApplicationServer的實現(xiàn)類
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 下創(chuàng)建名為 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。 現(xiàn)階段只有 jboss 應用服務器的實現(xiàn),在以后的版本中會陸續(xù)增加如 weblogic、glassfish 等應用服務器的實現(xiàn)。如果現(xiàn)在需要 jboss 之外的實現(xiàn),可實現(xiàn) cn.agrael.struts.plugin.ejb3.ApplicationServer 接口,并使用 struts-ejb3-plugin.properties 修改 ejbContainer 為實現(xiàn)類。
