为了实现向struts2的Action中注入ejb3,我写了一个Annotation用来定义要注入哪个ejb3,又写了一个struts2的interceptor用来实施注入。Annotation代码如下:
@Retention(value=RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD})
@Inherited
public @interface JndiLookup {
String jndi();
Class type();
}
Interceptor代码如下:
public class JndiLookupInterceptor implements Interceptor {
private InitialContext contaxt;
public void destroy() {
;
}
public void init() {
try {
contaxt = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
}
public String intercept(ActionInvocation invocation)
throws Exception {
Method[] methods = invocation.getAction()
.getClass().getMethods();
for(Method method : methods) {
try{
Class[] parameterTypes
= method.getParameterTypes();
if(parameterTypes.length != 1) {
continue;
}
JndiLookup jndiLookup
= method.getAnnotation(JndiLookup.class);
if(jndiLookup == null) {
continue;
}
if(jndiLookup.jndi() == null
|| jndiLookup.jndi().length() == 0) {
continue;
}
if(jndiLookup.type() == null) {
continue;
}
Object object = this.contaxt.lookup(jndiLookup.jndi());
method.invoke(invocation.getAction(),
new Object[] {jndiLookup.type().cast(object)});
} catch (Exception e) {
e.printStackTrace();
;
}
}
return invocation.invoke();
}
}
需要被注入的Action代码如下:
public class SomeAction {
private DataSource ds;
@JndiLookup(jndi="jdbc/mysqlDS", type=DataSource.class)
public void setDs(DataSource ds) {
this.ds = ds;
}
public String execute() {
//do something about the ds
}
}
经测试,在tomcat下注入DataSource成功了。看来Annotation和Struts2真的是个好东西呀!
分享到:
相关推荐
用注解(annotation)方式配置struts2 (webwork) 中action的拦截器(interceptor)
struts2 annotation 注解的详细介绍及使用方法
struts2-Annotation
SSH2 annotation 实现struts2.1.6 spring2.5.6 hibernate3.3 全注解开发 hibernate延迟加载_懒加载 具体应用
struts2 使用Annotation配置实现的小例子,转载供大家学习使用,里面包括了所需的jar包
参考博文:http://blog.csdn.net/u011638419/article/details/41743049
本文档对利用struts2 注解 annotation 实现文件下载作了简单介绍,并有代码为例,希望对学习struts2注解annotation的人有帮助,特别是需要动态传参方面。
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
annotation hibernate struts spring springsecurity API中英文均有 从各大网站下载足足花了我50积分
马士兵视频里的struts2 hibernate3 spring2.5 annotation 整合 绝对好用 里面还有jar包 部署就可以用
十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...
struts2,json-plugin, 并全注解零配置
博文链接:https://flym.iteye.com/blog/174358
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发
Struts2 Spring Hibernate 框架整合 Annotation Maven project
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5图文教程整合开发.doc
NULL 博文链接:https://yufengweb.iteye.com/blog/775717
Struts2的“零配置”特性是Struts2的新功能,可能会出现一些小Bug,所以企业开发者请慎重使用该特性, ...如果用的是Annotation的Struts2,就要将struts.xml去掉,否则即使将struts.xml中的内容注销,也会报错;
本Demo基于annotation的Struts2.1.8+Spring2.5.6+Hibernate3.3.2的整合