`
Agrael
  • 浏览: 14815 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

通过struts2-ejb3-plugin把Struts2与EJB3.0无缝整合起来

阅读更多

        本文是讲述使用struts2-ejb3-plugin这个插件将Struts2与EJB3.0进行整合开发。
你可以从Apache的Struts2的Plugin列表了解它:
http://cwiki.apache.org/S2PLUGINS/home.html

也可直接进入主页了解它:
http://cwiki.apache.org/S2PLUGINS/struts2-ejb3-plugin.html
或者从该Google代码:
http://code.google.com/p/struts2-ejb3-plugin/
        Struts2是时下比较流行的JAVA EE框架,而EJB为JAVA企业平台标准规范并且是JAVA EE体系的核心技术,不过前几年由于EJB2.1的烦琐以及spring、hibernate等轻量级框架的推广流行,EJB2.1大有被取代的趋势。在这种情况下,吸取了spring、hibernate等框架的优点的EJB3.0也就顺理成章的诞生,不过经过了漫长的发布过程后,貌似天下已经是spring、hibernate等轻量级框架的了,在中国国内更是满国尽是SSH(我自己也觉得说得夸张了点^_^)。EJB3.0貌似在中国的关注度并不是很高,也好象并没有多少公司多少人使用它,这也造成了EJB3.0的中文资料比较少,而与像Struts2这样的优秀的框架整合的资料就更少了。曾经在一个项目中决定使用Struts2与EJB3.0,在Struts2的Action与Interceptor里使用context.lookup来查找EJB组件是多么的痛苦。四处寻找好的整合方案,却空手而回。Apache的Struts2的Plugin列表里有两个EJB3.0的Plugin,可以通过注解完成整合,不过因为引入了第三方类库与必须是写死的引用路径而最终放弃了。最后,写了一种模拟应用服务器的规则以及EJB组件生命周期的Plugin,struts2-ejb3-plugin就是这个Plugin。

 

上面废话了这么多-。-下面进入正题。

插件的安装:

struts2-ejb3-plugin的安装比较简单,拖Struts2优秀的Plugin机制的福,只需要将struts2-ejb3-plugin.jar放到 /WEB-INF/lib 目录中就可以了。 除了plugin本身的配置以外,还需要在 classpath 下创建名为 jndi.properties 的资源文件用做 jndi配置
,plugin 中使用到的jndi查找依赖于该配置。

 

代码示例:

 

先是几个SessionBean

 

@Remote
public interface DemoRemote extends Serializable {

	void remoteMethod();
}

  

@Local
public interface DemoLocal {

	void localMethod();
}

 

@Stateless(name = "ejb/demo1")
public class DemoBean implements DemoLocal, DemoRemote {

	private static final long serialVersionUID = -779739898907524857L;

	public void localMethod() {
		System.out.println("DemoBean本地方法。");
	}

	public void remoteMethod() {
		System.out.println("DemoBean远程方法。");
	}
}

 

@Stateless(name = "ejb/demo2")
public class DemoBean2 implements DemoLocal {

	public void localMethod() {
		System.out.println("DemoBean2本地方法。");
	}
}

 

@Remote
public interface DemoMethodInjectRemote extends Serializable{

	void method();
}

 

@Local
public interface DemoMethodInjectLocal {

	void method();
}

 

@Stateless
public class DemoMethodInjectBean implements DemoMethodInjectLocal,
		DemoMethodInjectRemote {

	private static final long serialVersionUID = 806301363823340506L;

	public void method() {
		System.out.println("DemoMethodInjectBean");
	}
}

 

 

下面是Interceptor

 
public class DemoInterceptor1 {

	@AroundInvoke
	public Object interceptorMethod(InvocationContext invocationContext) throws Exception {
		System.out.println("拦截器1。");
		return invocationContext.proceed();
	}
}

  

public class DemoInterceptor2 {

	@AroundInvoke
	public Object interceptorMethod(InvocationContext invocationContext) throws Exception {
		System.out.println("拦截器2。");
		return invocationContext.proceed();
	}
}

 

 

SessionBean 与 Interceptor 都会在执行到它们的时候打印出自己的信息。

 

 

下面是本文的重点,Struts2的Action

 

@Interceptors(DemoInterceptor1.class)
public class DemoAction extends ActionSupport {

	private static final long serialVersionUID = -472366623817152071L;

	@EJB
	private DemoRemote demoRemote;

	// /////
	// 为 glassfish2时,要将本地接口的注入注释掉,glassfish不支持本地接口在web层调用,如果不注释掉会在部署时报错。
	@EJB
	private DemoLocal demoLocal;
	// /////

	private DemoMethodInjectRemote demoMethodInjectRemote;

	@EJB(beanName = "ejb/demo1")
	private DemoRemote demo1;

	@EJB(beanName = "ejb/demo1")
	private DemoRemote demo2;

	@EJB
	public void setDemoMethodInjectRemote(
			DemoMethodInjectRemote demoMethodInjectRemote) {
		this.demoMethodInjectRemote = demoMethodInjectRemote;
	}

	@PostConstruct
	@Interceptors(DemoInterceptor2.class)
	@ExcludeClassInterceptors()
	public void init() {
		System.out.println("初始化。");
	}

	@Interceptors( { DemoInterceptor2.class, DemoInterceptor1.class })
	@Override
	public String execute() throws Exception {
		demoRemote.remoteMethod();
		demoLocal.localMethod();
		demoMethodInjectRemote.method();
		demo1.remoteMethod();
		demo2.remoteMethod();
		return SUCCESS;
	}

}

 

 Action中通过EJB的注解注入前面的SessionBean,并且支持除全局默认拦截器注解外的拦截器相关注解。对于生命周期注解,目前只支持@PostConstruct。struts2-ejb3-plugin的好处是通过模拟应用服务器规则与模拟EJB组件生命周期完成整合,无须引入第三方类库。

 

对于Struts2自带的拦截器,struts2-ejb3-plugin也能够像Action一样正常工作。在Action的execute(或者自定义的名称)方法中同时使用struts2的Interceptor和@Interceptors 时,@Interceptors会在Interceptor之前开始,在Interceptor之后结束。

 

下面看个 Struts2的Interceptor

 

 
public class DemoStrutsInterceptor implements Interceptor {

	private static final long serialVersionUID = 3525334943511726314L;

	@EJB
	private DemoRemote demoRemote;
	
	public void destroy() {

	}

	public void init() {
		System.out.println("struts拦截器初始化。");
	}
	
	@PostConstruct
	@Interceptors(DemoInterceptor1.class)
	public void pluginInit(){
		System.out.println("struts2-ejb3-plugin初始化。");
	}

	@Interceptors(DemoInterceptor2.class)
	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("Struts拦截器");
		demoRemote.remoteMethod();
		return arg0.invoke();
	}

}

 可以看出,对Struts2的拦截器不仅可以使用注入,还可以对它使用生命周期以及拦截器的注解(拦截器的拦截器。。。恩,有点绞。。^_^)。

 

 

 

 

 

对配置文件的一些说明

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
的资源文件覆盖默认的配置。

 

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

 

关于Struts2与EJB3.0的整合就写到这里了,附件里有完整的DEMO,因为比较简单,所以没写注释,希望大家见谅。

因为本人经验及水平有限,如有疏漏或者是错误的地方,希望大家多多拍砖^_^。

分享到:
评论
12 楼 zhmore 2010-11-10  
环境:jboss5.1 struts2.2.1
在EAR内interceptor工作正常,但是@EJB注解无效,没有任何提示。楼主能否解决下。
11 楼 Agrael 2010-06-01  
struts2.0.x和2.1.x很多地方不一样,我抽取源码重新打了个针对2.0.x的包struts2.0.x-ejb3-plugin.jar。在地址http://code.google.com/p/struts2-ejb3-plugin/downloads/list有。
10 楼 Agrael 2010-06-01  
struts2.0.14的xwork2.0.7里没有com.opensymphony.xwork2.util包,会报java.lang.NoClassDefFoundError。
貌似就是这一点的问题。其他都应该是兼容的。

用2.1.8.1时,报的什么错?
9 楼 mmwy 2010-06-01  
什么都不报,在jboss的server.log日志文件里面搜“cn.agrael”也没东西。

然后我把struts2.0.14的jar包换成2.1.8.1的,同时改web.xml,将2.0.14的org.apache.struts2.dispatcher.FilterDispatcher换成org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,再启动就开始有反应(报错)了。


注:
struts2.0.14用的jar包
struts2-core-2.0.14.jar
ognl-2.6.11.jar
xwork-2.0.7.jar
freemarker.jar

2.1.8.1时用的jar包
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
8 楼 Agrael 2010-06-01  
2.014会报什么?
7 楼 mmwy 2010-06-01  
折腾了半天,最后发现:这东西只支持struts2.1.x,我用的struts2.0.14压根不支持。
6 楼 Agrael 2010-06-01  
mmwy 写道
我们项目是struts2、ejb3架构,服务器用jboss4.2.3,今天才发现struts2-ejb3-plugin,用的时候发现有如下问题:

如果一个webapp内,要使用两个及以上的ear中部署的ejb,怎么办?


附件重新上传过了。以前那个DEMO里的lib版本比较旧,现在有个比较新的覆盖了。
你使用的是Jboss4.x,不知道和Jboss5.x的规则是否一样。plugin的Jboss实现是5.x的。
5 楼 Agrael 2010-06-01  
mmwy 写道
我们项目是struts2、ejb3架构,服务器用jboss4.2.3,今天才发现struts2-ejb3-plugin,用的时候发现有如下问题:

如果一个webapp内,要使用两个及以上的ear中部署的ejb,怎么办?


在JBOSS里,如果是2个以上的,可以使用ejb的多ear的规则指定路径。
如:
@EJB(beanName="one-ejb.jar#xxxEJB")
4 楼 mmwy 2010-06-01  
我们项目是struts2、ejb3架构,服务器用jboss4.2.3,今天才发现struts2-ejb3-plugin,用的时候发现有如下问题:

如果一个webapp内,要使用两个及以上的ear中部署的ejb,怎么办?

3 楼 supwinebibber 2010-04-22  
研究下底层的代码,非常值得研究和学习。狂顶!
2 楼 Agrael 2010-04-17  
呵呵,谢谢楼上的。
1 楼 sqzhangling 2010-04-16  
还没试过,有机会试试,LZ的探索精神值得学习的。

相关推荐

    struts2.1.6+spring2.0+hibernate3.2常用配置包

    最近温习ssh2整合编程,顺便浏览下struts2有什么更新的消息,下载了新版本的struts2的2.1.8.1版,使用的是MyEclipse8.0开发,但是问题就随之而来了。MyEclipse8.0中自带的struts2版本是2.1.6,spring版本有2.0,2.5...

    ssh框架所需jar包

    junit-4.5.jar,mysql-connector-java-3.1.13-bin.jar,ognl-2.6.11.jar,slf4j-api-1.5.8.jar,slf4j-nop-1.5.8.jar,spring.jar,struts2-convention-plugin-2.1.6.jar,struts2-core-2.1.6.jar,struts2-spring-plugin-...

    web项目常用jar包及说明.zip

    10.struts2-spring-plugin-2.1.8.jar(struts2与spring集成时使用的) Spring需要的jar包: 1.spring.jar(里面含有spring的所有核心类库) 2.commons-logging-1.1.1.jar(ASF出品的日志包,struts2 2、spring、...

    SSH 项目 整合jar包

    10.struts2-spring-plugin-2.1.8.jar(struts2与spring集成时使用的) 二、Spring需要的jar包: 1.spring.jar(里面含有spring的所有核心类库) 2.commons-logging-1.1.1.jar(ASF出品的日志包,struts2 2、spring、...

    SSH 框架所需JAR包

    10.struts2-spring-plugin-2.1.8.jar(struts2与spring集成时使用的) Spring需要的jar包: 1.spring.jar(里面含有spring的所有核心类库) 2.commons-logging-1.1.1.jar(ASF出品的日志包,struts2 2、spring、...

    Java Web开发常用jar工具集

    包含ipseeker-1.0.jar,derby.jar, jsp-api.jar, javax.servlet.jar, annotations-api.jar, jstl.jar, log4j-1.2.17.jar, mysql-connector-java-5.1.7-bin.jar, servlet-api.jar, standard.jar, struts2-dojo-plugin-...

    网上购物系统

    数据库书写论文代码讲解网上购物系统... <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-spring-plugin-2.0.11.1.jar"/> <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xerces-2.6.2.jar"/>

    Struts in Action中文版

    2. 深入 STRUTS架构..................................................................................37 2.1. 随便谈谈......................................................................................

    struts in Action

    2. 深入STRUTS 架构..................................................................................37 2.1. 随便谈谈.......................................................................................

    超级有影响力霸气的Java面试题大全文档

     封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。 4. 多态性: ...

    java 面试题 总结

    封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。 4. 多态性: 多...

Global site tag (gtag.js) - Google Analytics