[英]When should I use POJO (instead of EJB) in a Java EE application?
I am currently learning JAVA EE. 我目前正在学习JAVA EE。 I use an oracle Java EE 7 tutorial for that.
我使用oracle Java EE 7教程。 According to this tutorial, section 34.1.4, they used some non-EJB helper classes for the tutorial example.
根据本教程的第34.1.4节,他们在教程示例中使用了一些非EJB辅助类。 http://docs.oracle.com/javaee/7/tutorial/doc/ejb-basicexamples001.htm
http://docs.oracle.com/javaee/7/tutorial/doc/ejb-basicexamples001.htm
I'm wondering in what cases I should make a class EJB, and in what cases I should use a usual helper class. 我想知道在什么情况下我应该创建一个类EJB,在什么情况下我应该使用通常的帮助器类。 I have read what the benefits of using EJB are.
我已经了解了使用EJB的好处。 But are there cases when it's better to use POJOs?
但是有没有使用POJO更好的情况?
The short answer is "always unless you need specific EJB facilities". 简短的回答是“除非您需要特定的EJB工具”。
The longer answer is the following. 更长的答案如下。 Years ago when EJB prior 3.0 were used the EJB was something "heavy".
多年前,当使用EJB之前的3.0时,EJB是“沉重的”。 You had to implement interface, inherit your bean from base class etc. Shortly EJB could be used in container only.
你必须实现接口,从基类继承你的bean等。很简单,EJB只能在容器中使用。 This means that writing unit tests for EJB was very difficult.
这意味着为EJB编写单元测试非常困难。
So, people used the following strategy. 因此,人们使用以下策略。 They implemented everything they can in POJOs wrapped when needed by EJBs.
他们在EJB需要时在POJO中实现了所有可能的功能。 The solution was verbose (because some stuff was duplicated) but very modular and better testable.
解决方案很冗长(因为有些东西是重复的)但非常模块化且更易于测试。
Since EJB 3.0 were introduced there is (almost) no difference between POJO or EJB. 自从引入EJB 3.0以来,POJO或EJB之间几乎没有区别。 Actually EJB is a POJO with some annotations.
实际上,EJB是一个带有一些注释的POJO。 This means that you do not have to create POJO and then wrap it with thin EJB layer.
这意味着您不必创建POJO,然后使用瘦EJB层将其包装起来。 You can just implement everything in your class (call it POJO if you want) and then turn it into EJB using annotations.
您可以在类中实现所有内容(如果需要,可以将其称为POJO),然后使用注释将其转换为EJB。 Still as delegation is good, so as more code is EJB-annotations-less as better.
仍然因为委托是好的,所以更多的代码是EJB注释 - 越少越好。
EJBs components are managed (by the container) which implies some extra overhead. 管理EJB组件(由容器管理),这意味着一些额外的开销。 There is an antipattern called Sledgehammer for a Fly :
Fly有一个名为Sledgehammer的反模式:
Describes when EJB, a technology that comes with extra overhead, is chosen over a simple POJO where only lightweight processing is the requirement. 描述了EJB(一种带有额外开销的技术)是在简单的POJO上选择的,其中只需要轻量级处理。 Generates additional complexity;
产生额外的复杂性; no apparent benefit
没有明显的好处
The solution: 解决方案:
If your code does not use the following container services, use a POJO : 如果您的代码不使用以下容器服务,请使用POJO :
I would add that in many cases, EJBs are used like Service Facades / Services. 我想补充一点,在很多情况下,EJB就像Service Facades / Services一样使用。 There are real world scenarios when you want to process business logic using a design pattern (CDI objects or POJOS) instead of just put the logic in a procedural way using only EJBs.
当您想要使用设计模式(CDI对象或POJOS)处理业务逻辑而不是仅使用EJB以过程方式放置逻辑时,存在真实场景。 Rephrasing : An EJB service facade is the single entry point to a design pattern which resolves complex business needs (if you no need a design pattern don't use it, keep it simple!).
重新定义:EJB服务外观是解决复杂业务需求的设计模式的单一入口点(如果您不需要设计模式,请不要使用它,保持简单!)。
Sources: Sledgehammer for a Fly, Service Facade, Service: 资料来源:飞行大锤,服务门面,服务:
OCM Java EE 6 Enterprise Architect OCM Java EE 6 Enterprise Architect
Real World Java EE Patterns--Rethinking Best Practices 真实世界的Java EE模式 - 重新思考最佳实践
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.