Spring vs EJB(优缺点)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29080031/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:27:15  来源:igfitidea点击:

Spring vs EJB (advantage and disadvantage)

springjakarta-eeejb-3.1

提问by Informatique2015

I am currently preparing my final project study. I have to develop a distributed application for the management of human resources, but on my benchmark for technologies to use I found it confusing to decide between Spring or the EJB 3.1.

我目前正在准备我的最终项目研究。我必须开发一个分布式应用程序来管理人力资源,但是根据我使用的技术基准,我发现在 Spring 还是 EJB 3.1 之间做出决定很令人困惑。

I do not know which will be the best suitable and easiest to use. I am a beginner for both technologies and therefore I hope so someone can help me.

我不知道哪个是最合适和最容易使用的。我是这两种技术的初学者,因此我希望有人可以帮助我。

回答by akhilless

You do not choose between EJB and Spring, you actually choose between Java EE and Spring because EJB is only a part of Java EE which includes also other components like JMS, Servlets, JSP, JSF, CDI, etc. In its early days (J2EE 1.4) programming model of enterprise edition of Java had a lot of flaws and deficencies, but the most important ones were that it was tedious and verbose with a lot of boiler-plate code and tons of xml configurations. Spring was introduced as an alternative framework favoring convention-over-configuration principle. In other words, in Spring there were some sensible defaults that could be reconfigured if you need it. Java EE 5 introduced significant changes adopting the same convention-over-configuration principle from Spring drastically reducing code amount and complexity required to get things going, so in this area Spring does not hold any sensisble advantage any more.

您不必在 EJB 和 Spring 之间进行选择,您实际上是在 Java EE 和 Spring 之间进行选择,因为 EJB 只是 Java EE 的一部分,它还包括其他组件,如 JMS、Servlets、JSP、JSF、CDI 等。在其早期(J2EE 1.4)Java企业版的编程模型有很多缺陷和不足,但最重要的是它繁琐冗长,大量样板代码和大量xml配置。Spring 被引入作为支持约定优于配置原则的替代框架。换句话说,在 Spring 中有一些合理的默认值,如果需要,可以重新配置。Java EE 5 采用与 Spring 相同的约定优先于配置原则引入了重大更改,大大减少了使事情顺利进行所需的代码量和复杂性,

It is also important to bear in mind that Java EE is just a specification. To build a real world app you need an implementation and there are plenty of them today - Glassfish, JBoss, TomcatEE, etc. all provide different implementations of Java EE specification introducing additional complexity - I mean, now you have to choose which Java EE implementation to choose. You can constrast that with Spring which comes from a single source.

同样重要的是要记住 Java EE 只是一个规范。要构建真实世界的应用程序,您需要一个实现,现在有很多实现——Glassfish、JBoss、TomcatEE 等都提供了 Java EE 规范的不同实现,从而引入了额外的复杂性——我的意思是,现在您必须选择哪种 Java EE 实现选择。您可以将其与来自单一来源的 Spring 进行对比。

Both frameworks gives you pretty much similar functionality. All support transactions, ORM, provide tools for building business logic, support CDI, AOP. In both Spring and Java EE you can only use the parts that you need, in other words you don't have to use the whole framework. You can even use them together - they can interoperate. Thanks to introduction of embeddable containers you now can even use Java EE features you need in areas like desktop appplications which traditionally were the realm of Spring.

这两个框架都为您提供了非常相似的功能。所有支持事务,ORM,提供构建业务逻辑的工具,支持CDI,AOP。在 Spring 和 Java EE 中,您只能使用您需要的部分,换句话说,您不必使用整个框架。您甚至可以一起使用它们 - 它们可以互操作。由于引入了可嵌入容器,您现在甚至可以在传统上属于 Spring 领域的桌面应用程序等领域使用您需要的 Java EE 功能。

But one area where Java EE still lacks behind Spring is comfortable testability. It is not easy to write unit tests for EJB - for this you have to use a special third-party framework (Arquillian) and write some boilerplate code inside your tests (e.g. for building the test deployment package and deploying it onto the container, etc.). In fact, Java EE lacks any support for testing EJBs out of the box. In contrast, Spring is buuild with testability and TDD in mind. Testing Spring beans is easy since Spring includes bundled support for both testing (unit testing, integration testing) of all parts of the application as well as mocking.

但是 Java EE 在 Spring 之后仍然缺乏的一个领域是舒适的可测试性。为 EJB 编写单元测试并不容易 - 为此,您必须使用特殊的第三方框架 (Arquillian) 并在测试中编写一些样板代码(例如,用于构建测试部署包并将其部署到容器等) .) 事实上,Java EE 缺乏对开箱即用的 EJB 测试的任何支持。相比之下,Spring 在构建时考虑了可测试性和 TDD。测试 Spring bean 很容易,因为 Spring 包括对应用程序所有部分的测试(单元测试、集成测试)以及模拟的捆绑支持。