Java Spring 与 EJB。Spring能代替EJB吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1779169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring vs EJB. Can Spring replace EJB?
提问by cometta
采纳答案by duffymo
Spring was developed as an alternative to EJB right from its inception, so the answer is of course you can use Spring in place of EJBs.
Spring 从一开始就被开发为 EJB 的替代品,因此答案当然是您可以使用 Spring 代替 EJB。
If there's an "advantage" to using EJBs, I'd say that it would depend on the skills of your team. If you have no Spring expertise, and lots of EJB experience, then maybe sticking with EJB 3.0 is a good move.
如果使用 EJB 有“优势”,我会说这取决于您团队的技能。如果您没有 Spring 专业知识,并且有很多 EJB 经验,那么坚持使用 EJB 3.0 可能是一个不错的选择。
App servers written to support the EJB standard can, in theory, be ported from one compliant Java EE app server to another. But that means staying away from any and all vendor-specific extensions that lock you in to one vendor.
为支持 EJB 标准而编写的应用服务器理论上可以从一个兼容的 Java EE 应用服务器移植到另一个。但这意味着远离任何和所有将您锁定到一个供应商的特定于供应商的扩展。
Spring ports easily between app servers (e.g., WebLogic, Tomcat, JBOSS, etc.) because it doesn't depend on them.
Spring 可以轻松地在应用服务器(例如 WebLogic、Tomcat、JBOSS 等)之间进行移植,因为它不依赖于它们。
However, you are locked into Spring.
但是,您被锁定在 Spring 中。
Spring encourages good OO design practices (e.g., interfaces, layers, separation of concerns) that benefit any problem they touch, even if you decide to switch to Guice or another DI framework.
Spring 鼓励良好的 OO 设计实践(例如,接口、层、关注点分离),这些实践有利于他们触及的任何问题,即使您决定切换到 Guice 或其他 DI 框架。
Update: This question and answer are five years old in 2014. It needs to be said that the world of programming and application development have changed a great deal in that time.
更新:这个问题和答案是 2014 年的五年。需要说的是,编程和应用程序开发的世界在那段时间发生了很大的变化。
It's no longer just a choice between Java or C#, Spring or EJBs. With vert.xit's possible to eschew Java EE altogether. You can write highly scalable, polyglot applications without an app server.
它不再只是 Java 或 C#、Spring 或 EJB 之间的选择。使用vert.x可以完全避开 Java EE。您可以在没有应用服务器的情况下编写高度可扩展的多语言应用程序。
Update: It's Mar 2016 now. Spring Boot offers an even better way to write applications without Java EE app servers. You can create an executable JAR and run it on a JVM.
更新:现在是 2016 年 3 月。Spring Boot 提供了一种更好的方法来编写没有 Java EE 应用服务器的应用程序。您可以创建一个可执行 JAR 并在 JVM 上运行它。
I wonder if Oracle will continue to support the Java EE spec. Web services have taken over for EJBs. The EJB solution is dead. (Just my opinion.)
我想知道 Oracle 是否会继续支持 Java EE 规范。Web 服务已经接管了 EJB。EJB 解决方案已死。(只是我的观点。)
回答by Pascal Thivent
First, let me say it clearly, I'm not saying you shouldn't use Spring but, because you are asking for some advantages, here are at least two of them:
首先,让我说清楚,我不是说你不应该使用 Spring,但是,因为你要求一些优势,这里至少有两个:
EJB 3 is a standard while Spring is not (it's a de facto standard but that's not the same thing) and this won't change in the foreseeable future. Although you can use the Spring framework with any application server, Spring applications are locked into both Spring itself and the specific services you choose to integrate in Spring.
The Spring framework sits on top of the application servers and service libraries. Service integration code (e.g. data access templates) resides in the framework and is exposed to the application developers. In contrast, the EJB 3 framework is integrated into the application server and the service integration code is encapsulated behind an interface. EJB 3 vendors can thus optimize the performance and developer experience by working at the application server level. For example, they can tie the JPA engine closely to JTA transaction management. Another example is clustering support which is transparent to EJB 3 developers.
EJB 3 是一个标准,而 Spring 不是(它是一个事实上的标准,但这不是一回事)并且在可预见的未来这不会改变。尽管您可以将 Spring 框架与任何应用程序服务器一起使用,但 Spring 应用程序被锁定在 Spring 本身和您选择在 Spring 中集成的特定服务中。
Spring 框架位于应用服务器和服务库之上。服务集成代码(例如数据访问模板)驻留在框架中并向应用程序开发人员公开。相比之下,EJB 3 框架集成到应用服务器中,服务集成代码封装在一个接口后面。因此,EJB 3 供应商可以通过在应用程序服务器级别工作来优化性能和开发人员体验。例如,他们可以将 JPA 引擎与 JTA 事务管理紧密联系起来。另一个示例是对 EJB 3 开发人员透明的集群支持。
EJB 3 is not perfect though, it is still lacking some features (e.g. injection of non managed components like simple POJOs).
虽然 EJB 3 并不完美,但它仍然缺乏一些特性(例如注入非托管组件,如简单的 POJO)。
回答by Bozho
Pascal's points are valid. There are, however, the following in favour of Spring.
帕斯卡的观点是有效的。但是,有以下支持 Spring 的情况。
EJB specification is actually a bit loose, and therefore different behaviours can be observed with different application servers. This will not be true for the most cases, of course, but I've had such a problem for some "dark corners".
Spring has a lot of extra goodies, like spring-test, AOP, MVC, JSF integration, etc. EJB has some of those (interceptors, for example), but in my opinion they are not that much developed.
EJB 规范实际上有点松散,因此在不同的应用程序服务器上可以观察到不同的行为。当然,在大多数情况下这不是真的,但我在某些“暗角”上遇到过这样的问题。
Spring 有很多额外的好处,比如 spring-test、AOP、MVC、JSF 集成等。 EJB 有一些(例如拦截器),但在我看来,它们并没有那么发达。
In conclusion, it depends mainly on your exact case.
总之,这主要取决于您的具体情况。
回答by sasi
Spring is meant to complement EJB, not to replace it. Spring is a layer on top of EJB. As we know, coding of EJB is done using API, which means we have to implement everything in APIs using the Spring framework. We can create boiler-plate code, then just take that plate, add some stuff to it, then everything's done. Internally Spring is connected with EJB -- Spring wouldn't exist without EJB.
Spring 旨在补充 EJB,而不是取代它。Spring 是 EJB 之上的一层。我们知道,EJB 的编码是使用 API 完成的,这意味着我们必须使用 Spring 框架在 API 中实现所有内容。我们可以创建样板代码,然后拿起那个盘子,添加一些东西,然后一切都完成了。Spring 在内部与 EJB 相关联——如果没有EJB,Spring 就不会存在。
The main advantage of using Spring is that there is no coupling at all between classes.
使用 Spring 的主要优点是类之间根本没有耦合。