问题描述
是否有人知道是否可以在Java EE应用程序中使用EJB3 bean迭代替换EJB2.1 bean?
does anybody know if it is possible to iteratively replace EJB2.1 beans with EJB3 beans in Java EE application?
这是:一次从代码中删除一个2.1 bean并添加相应的EJB3 bean,实现相同的行为而不触及其余的代码(+能够通过新EJB3中的注释注入遗留EJB。
That is: at one time remove one 2.1 bean from the code and add corresponding EJB3 bean that implements the same behavior without touching the rest of the code (+ be able to inject the legacy EJBs via annotations in the new EJB3).
我不是EJB规范的专家(我只有EJB3的经验),但对我来说EJB是一个简单的组件,具有由appserver管理的给定业务接口。 AFAIK EJB3带来了很大的简化,如何编写组件(没有人工界面),并且大多数时候由于注释可以省略xml描述符,但基础是相同的。所以看起来似乎有道理,它可以工作。
I am not expert at EJB specs (and I have experience only with EJB3), but to me EJB is a simply component with given business interface that is managed by the appserver. AFAIK EJB3 brought big simplification how to write the component (no artifical interfaces) and most of the time xml descriptor can be omitted thanks to the annotations, but the basics are the same. So it is seem plausible, it could work.
EJB2.1&是否存在任何不兼容性。 EJB3?
Is there any incompatibility between EJB2.1 & EJB3?
问题的核心是迁移EJB2.1 - > EJB3是否需要停止世界/完全重写操作或者可以做到同时添加新功能并修复遗留应用程序中的错误(因此在运行的应用程序中会有一段时间混合使用EJB2.1和EJB3)。
The core of the question is if migration EJB2.1 --> EJB3 needs to be stop-the-world/complete-rewrite operation or one can do it while adding new feature and fixing bugs to the legacy application (so there will be mix of EJB2.1 and EJB3 for some time in the running app).
编辑:
- 我只对会话bean感兴趣。
- 我很好奇查找是否有效(以及如何)。 AFAIK EJB2.1需要一个称为home接口的东西来获取对不同EJB的引用,但是EJB3没有home接口......
推荐答案
EJB 2.1和EJB 3 bean的共存
EJB2和EJB3 bean可以在一个企业应用程序(.ear)中共存,但是不能驻留在同一个ejb jar文件(模块)上。因此,EJB3 bean必须驻留在与EJB2 bean不同的jar中。
Coexist of EJB 2.1 and EJB 3 beans
EJB2 and EJB3 beans can coexist in one enterprise application (.ear), but can’t reside on the same ejb jar file (module). Hence, the EJB3 beans must reside in a different jar to the EJB2 beans.
EJB3 bean没有home接口,而EJB 2.1需要它。为了使EJB3 bean能够从EJB2访问,您需要将本地home接口(或者如果需要远程调用,则为远程home)添加到EJB3 bean。
EJB3 bean hasn't home interface while EJB 2.1 requires it. In order to make EJB3 bean being able to access from EJB2, you need to add local home interface (or remote home if remote call is required) to EJB3 bean.
创建home接口:
public interface SystemTimeLocalHome extends EJBLocalHome {
SystemTimeLocal create() throws CreateException;
}
将主界面添加到EJB3 bean:
Add home interface to EJB3 bean:
@Stateless
@Local(TimeServiceLocal.class)
@LocalHome(TimeServiceLocalHome.class)
public class TimeServiceBean implements TimeServiceLocal {
public long getCurrentTimeMillis() {
return System.currentTimeMillis();
}
}
在EJB2 bean内部调用EJB3 bean的代码遵循EJB2规范:查找引用,调用home接口创建本地接口,然后在本地接口上调用方法。
Inside EJB2 bean the code to invoke the EJB3 bean just follows EJB2 specification: looks up the reference, call the home interface to create local interface, then invoke method on the local interface.
Context ctx = new InitialContext();
TimeServiceLocalHome home = (TimeServiceLocalHome)ctx.lookup("java:comp/env/" + ejbRefName);
TimeServiceLocal timeService = home.create();
timeService.getCurrentTimeMillis();
从EJB 3调用EJB 2.1
依赖注入用于将EJB 2.1组件引用注入EJB3 bean。注入EJB3 bean的不同之处在于它是被注入的EJB2的home接口。在注入的EJB home接口上调用 create()
方法来实例化bean类。
Invoke EJB 2.1 from EJB 3
Dependency injection is utilized to inject the EJB 2.1 component references into EJB3 bean. The different to injecting EJB3 bean is that it's the home interface of EJB2 being injected. Call create()
method on injected EJB home interface to instantiate the bean class.
@EJB BankingServiceHome bsHome;
BankingService bs = bsHome.create();
bs.getBalance(...);
这篇关于EJB2和EJB3可以在一个应用程序中共存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!