- 浏览: 3503062 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wanglf1207:
EJB的确是个不错的产品,只是因为用起来有点门槛,招来太多人吐 ...
weblogic-ejb-jar.xml的元素解析 -
qwfys200:
总结的不错。
Spring Web Flow 2.0 入门 -
u011577913:
u011577913 写道也能给我发一份翻译文档? 邮件437 ...
Hazelcast 参考文档-4 -
u011577913:
也能给我发一份翻译文档?
Hazelcast 参考文档-4 -
songzj001:
DbUnit入门实战
Using Spring with EJB 3
By Robert. Filed in Java
|
Tags: ejb
, ioc
, spring
Back when we were planning the migration to Glassfish, I realised we would have two dependency-injection frameworks in use – EJB 3 and Spring. For obvious reasons, I wanted to know more about how these would interact. At the time (last July), I couldn’t find anyone who had used EJB 3 and Spring together – even Ben Alex from Interface21 hadn’t come across it. Six months later, and I still haven’t heard of anyone using Spring _from_ EJBs. Except for us.
There’s lots of reasons to use both EJB 3 and Spring. EJBs have
benefits Spring doesn’t give – integration with things like Servlets
and JSP tags, better integration with the container for monitoring and
management, tool support, and so on. Spring has benefits as well – most
notably, you don’t need the container, but also you get more control
over the bean lifecycle. Together, they are a good complement.
But how do you get a Spring-managed bean from an EJB? The answer: dependency injection, of course. That is, with the aid of a EJB Interceptor:
[source='java']
public class SpringBeanInterceptor {
private BeanFactory beanFactory;
@PostConstruct public void configureSpringBeans(InvocationContext context) throws Exception {
for (Method method : context.getTarget().getClass().getMethods()) {
if (method.isAnnotationPresent(SpringBean.class)) {
Class springClass = method.getParameterTypes()[0];
String springName = determineSpringBeanName(method, springClass);
Object springBean = beanFactory().getBean(springName);
try {
method.invoke(context.getTarget(), springBean);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
}
}
context.proceed();
}
private String determineSpringBeanName(Method method, Class springClass) {
String springName = method.getAnnotation(SpringBean.class).value();
if (springName.length() > 0) {
springName = toCamelCase(springClass.getSimpleName());
}
return springName;
}
private String toCamelCase(String string) {
return Character.toLowerCase(string.charAt(0)) + string.substring(1, string.length());
}
private BeanFactory beanFactory() {
if (beanFactory == null) {
BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(”classpath*:beanRefContext.xml”);
BeanFactoryReference ref = locator.useBeanFactory(”applicationContext-service”);
beanFactory = ref.getFactory();
}
return beanFactory;
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SpringBean {
String value() default “”;
}
[/source]
That’s it. This Interceptor will look over an EJB, after it’s
created, and provides the Spring beans. This version looks for
’setter-methods’ with an @SpringBean
annotation (you
could also change it to look for fields), and looks up a corresponding
bean from the Spring context (the ContextSingletonBeanFactoryLocator
makes this possible).
Here’s how you would use it:
[source='java']
@Stateless
@Interceptors(SpringBeanInterceptor.class)
public class MyServiceBean implements MyService {
private MySpringBean bean;
…
@SpringBean public void setMySpringBean(MySpringBean bean) {
this.bean = bean;
}
}
[/source]
With this (and one other trick I’ll discuss soon), you can (nearly) seamless integrate EJB3 and Spring.
The one big limitation of this: with Stateless Session Beans, you can only inject stateless dependencies, and you can only inject them when the EJB is initialised.
发表评论
-
说明SOA监管(SOA Governance)实例(收录备查)
2012-12-19 11:35 1750SOA 已经不是单纯技术问 ... -
Injecting Spring Beans into Java Servlets
2012-11-01 10:21 1936If you are working in a Java ... -
用 HttpServletResponseWrapper 实现 Etag 过滤器
2012-07-09 16:58 3759原文出处:http://blog.chenlb.com/200 ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:17 3884When I try to install the And ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:13 1988When I try to install the And ... -
[转]mybatis下的分页,支持所有的数据库
2011-07-21 13:21 14841大 家都知道,mybatis的自带分页方法只是逻 ... -
Java framework for text- & console-based forms?
2011-07-21 01:06 1711charva jcurses JNA , ... -
JNA(Java Native Access)学习入门
2011-07-21 01:04 22625Java Native Access 项目 在 ... -
使用IntrospectorCleanupListener 解决quartz引起的内存泄漏
2011-04-20 11:59 13362"在服务器运行过程中,Spring不停的运行的计划任 ... -
DBCP代码研读以及就数据库连接失效的解决
2011-03-31 11:03 3765问题 网上很多评论说DBCP有很多BUG,但是都没有指明是什 ... -
ContextLoaderListener
2010-12-06 15:58 8464(1) org.springframework.web.c ... -
Servlet3.0新功能: 异步处理
2010-12-06 15:22 3181J2EE 6和Glassfish 3V正式发 ... -
Servlet3.0引入的新特性
2010-12-06 15:20 3058Servlet3.0规范的新特性主要是为了3个目的: ... -
100個節點上運行群集亞馬遜EC2上Hazelcast
2010-12-03 23:59 3318本文的目的,適是给妳湮示的細節集群的100個節點。此湮示記錄, ... -
Spring Properties Reloaded
2010-12-02 14:54 4372Spring Properties Reloaded Som ... -
为spring2.5中的jpetstore增加perf4j监控
2010-09-02 13:51 2646perf4j是一款类似于log4j的性能检测工具. 它 ... -
语义网的学习资源大汇集(备忘)
2010-06-23 22:48 1734网上资源 http:/ ... -
使用 JOLAP 实现复杂分析查询
2010-06-06 13:42 1964Shashank Tiwari 在本文中对 ... -
HTML5 Canvas for Internet Explorer
2010-06-04 21:16 1857Canvascape http://www.benjoff ... -
大型网站架构演变和知识体系
2010-06-01 23:47 1970架构演变第一步:物 ...
相关推荐
在众多的持久化框架和技术中,JDO(Java Data Objects)作为一种非EJB的标准,提供了一种轻量级的数据持久化解决方案。本文旨在介绍如何将Spring框架与JDO结合起来使用,实现应用的透明持久性,并通过具体的实例展示...
* Spring enterprise: Spring Java EE integration, Spring Integration, Spring Batch, jBPM with Spring, Spring Remoting, messaging, transactions, scaling using Terracotta and GridGrain, and more. ...
Learn how to replace common EJB features with Spring alternatives, including Spring's comprehensive AOP-based transaction management framework. Learn to integrate and use these other open source ...
Agile Java™ Development With Spring, Hibernate and Eclipse is a book about robust technologies and effective methods which help bring simplicity back into the world of enterprise Java development....
Agile Java™ Development With Spring, Hibernate and Eclipse is a book about robust technologies and effective methods which help bring simplicity back into the world of enterprise Java development....
Develop, debug, test, and ... Creating Web Applications with Spring MVC Chapter 9. Creating Web Services Chapter 10. Asynchronous Programming with JMS Chapter 11. Java CPU Profiling and Memory Tracking
For example, when using JPA with OSGi, developers need to set up the persistence unit in a way that it can be dynamically managed by the OSGi framework. #### Creating OSGi Bundles from Existing Java...
在描述中提到的“Persistence Made Easy Using Java EE8, JPA and Spring”强调了通过使用Java EE8和JPA以及Spring框架来实现数据持久化的便捷性。这表明,本书可能会详细讲解如何利用这些技术简化数据库操作和数据...
- **Integration with Frameworks**: Examples of integrating XDoclet with popular web frameworks like Struts and Spring. #### Chapter 5: XDoclet and Web Frameworks Building on the previous chapter, ...
7. **《XML and Java: A Practical Guide to Using XML with Java for Data Exchange and Web Services》**:这本书讲解了如何使用Java处理XML数据,包括解析、生成、验证和转换XML文档的方法。 8. **《Patterns of...
3. Expression and Flow Control (表达式和流控制) 4. Array (数组) 5. Object-Oriented Programming I (类与对象I) 6. Object-Oriented Programming II (类与对象II) 7. Object-Oriented Advanced Topic (类与...
- Spring、Hibernate/JPA、EJB3、GWT、Hadoop、SOAP 和 RESTful Web Services 等高级主题也是课程内容的一部分。 在进行安卓网络编程教学和学习的过程中,要注重实践操作,因为理论知识与实际应用的结合非常重要。...
第四章 Working with Reusable Components(重用的组件)..241 JavaEE@xuxiang 4 Java/JavaEE学习笔记Jonny xuxiang5612@sina.com 第五章 Handling Exceptions(异常的处理)243 第六章 Advanced Topics(高级主题).........
第四章 Working with Reusable Components(重用的组件)..241 第五章 Handling Exceptions(异常的处理)243 第六章 Advanced Topics(高级主题)..........243 第七章 Custom Tags(自定义标记) .246 第八章 EL元素...
EJB 10.2.5. WAR 10.2.6. EAR 10.2.7. 其它打包类型 10.3. 通用生命周期目标 10.3.1. Process Resources 10.3.2. Compile 10.3.3. Process Test Resources 10.3.4. Test Compile 10.3.5. Test 10.3.6. ...