简体   繁体   English

我应该何时在Java EE应用程序中使用POJO(而不是EJB)?

[英]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

  • Concurrency 并发
  • Entity interaction 实体互动
  • Interceptors 拦截器
  • Life cycle management 生命周期管理
  • Timers 计时器
  • Transaction management 交易管理

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.

相关问题 我应该在哪里(确切地)在我的Java EE应用程序中使用同步 - Where (exactly) should i use synchronization in my Java EE application 来自Pojo的Java-EE远程调用ejb事务 - Java-ee remote calling ejb transaction from pojo 我应该使用哪个Java EE服务器? - Which Java EE server should I use? 为什么我应该使用Java EE服务器库而不是app libs? - Why should I use Java EE server libs instead of app libs? Java EE应用程序中的实用程序类应该是EJB的类还是具有静态方法的类 - Should utility classes in Java EE application be EJB's or classes with static methods 如何在Web Services Java EE中使用EJB - How to use an EJB in a Web Services Java EE Windows Server上的简单Java应用程序......我应该使用Java EE吗? - Simple Java app on a Windows Server … should I use Java EE 我应该如何在分层Java EE应用程序中设计插件系统? - How should I design a plugin system in a layered Java EE Application? 我应该在Java POJO上使用“扩展”来提取公共属性吗? - Should I use “extends” on Java POJO to extract common attribute? 我应该在Java EE网站中使用标准的自动处理方法吗? - Should I use standart autorization methods in Java EE site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM