简体   繁体   English

EJB的简单但良好的模式

[英]Simple but good pattern for EJB

What would you suggest as a good and practical but simple pattern for a solution with: 对于以下解决方案,您会建议一个好的,实用但简单的模式:

  • HTML + JSP (as a view/presentation) HTML + JSP(作为视图/演示文稿)
  • Servlets (controller, request, session-handling) Servlet(控制器,请求,会话处理)
  • EJB (persistence, businesslogic) EJB(持久性,businesslogic)
  • MySQL DB MySQL DB

And is it necessary to use an own layer of DAO for persistence? 是否有必要使用自己的DAO层来保持持久性? I use JPA to persist objects to my DB. 我使用JPA将对象持久保存到我的数据库中。

Should I withdraw business logic from my EJB? 我应该从EJB中撤销业务逻辑吗? All online sources tell me different things and confuses me... 所有在线资源都告诉我不同​​的事情并让我感到困惑......

I would definitely put the business logic in Stateless Session Beans. 我肯定会将业务逻辑放在无状态会话Bean中。 Stateless session beans are nice as they nicely capture the transaction boundaries. 无状态会话bean很好,因为它们可以很好地捕获事务边界。 And it decouples the View layer from the persistence layer. 它将View层与持久层分离。

Take care that the methods of the SSB correspond to small business goals the user wants to achieve. 注意SSB的方法对应于用户想要实现的小业务目标。

Another point is that you must be sure that the data you return has all data in the object tree and that you do not rely on lazy loading to get the rest, because this causes all kind of problems. 另一点是,您必须确保返回的数据包含对象树中的所有数据,并且您不依赖于延迟加载来获取其余数据,因为这会导致所有类型的问题。

Stay as far away as possible from Stateful Session Beans : they are bad news and are a broken concept in the context of a web application. 尽可能远离有状态会话Bean:它们是坏消息,在Web应用程序的上下文中是一个破碎的概念。

For long running things, consider using Message Driven Beans which you trigger by sending a JMS message. 对于长时间运行的事情,请考虑使用通过发送JMS消息触发的消息驱动Bean。 These are a nice way to do background processing which frees the business logic faster, keeps transactions shorter and returns control to the end user faster. 这些是进行后台处理的好方法,可以更快地释放业务逻辑,缩短事务处理并更快地将控制权交还给最终用户。

What would you suggest as a good and practical but simple pattern for a solution with JSP/Servlets + EJB + MySQL 对于使用JSP / Servlets + EJB + MySQL的解决方案,您会建议什么是一个优秀且实用但简单的模式

Use the MVC framework of your choice, Stateless Session Beans for the business logic and transaction management (prefer local interfaces if you don't need remoting), Entities for persistence. 使用您选择的MVC框架,无状态会话Bean用于业务逻辑和事务管理(如果您不需要远程处理,则更喜欢本地接口),持久性实体。

Inject your EJBs wherever possible (if you are using Java EE 6, this means anywhere and you can also skip the interface). 尽可能地注入EJB(如果您使用的是Java EE 6,这意味着在任何地方,您也可以跳过该接口)。

And is it necessary to use an own layer of DAO for persistence? 是否有必要使用自己的DAO层来保持持久性? I use JPA to persist objects to my DB. 我使用JPA将对象持久保存到我的数据库中。

Some might say yes, I say no in most cases. 有些人可能会说是,我在大多数情况下都说不。 The EntityManager already implements the Domain Store pattern, there is no need to shield it behind a DAO for simple needs . EntityManager已经实现了域存储模式,没有必要为了简单的需要而将它屏蔽在DAO后面。

You might want to read the following resources for more opinions on this: 您可能需要阅读以下资源以获取更多有关此内容的意见:

Should I withdraw business logic from my EJB? 我应该从EJB中撤销业务逻辑吗?

I wouldn't. 我不会。 Put your business logic in your (Stateless) Session Beans. 将您的业务逻辑放在您的(无状态)会话Bean中。 EJB3 are POJOs, they are easily testable, there is no need to delegate the business logic to another layer. EJB3是POJO,它们易于测试,不需要将业务逻辑委托给另一层。

Anno 2012 I would not recommend going for Servlets and JSP as the presentation layer. Anno 2012我不建议将Servlet和JSP作为表示层。 This was all the rage in 2002, but that's a decade ago. 这在2002年风靡一时,但那是在十年前。

Today Java EE has an excellent MVC framework build in called JSF. 今天,Java EE有一个名为JSF的优秀MVC框架。 You are much better off using this instead. 你改善使用它会好得多。 You most likely want to fetch some Widgets from the component library PrimeFaces, as all the standard ones are a bit basic. 你很可能想从组件库PrimeFaces中获取一些Widgets,因为所有标准的Widgets都有点基础。 A utility library like OmniFaces is a great addition as well. 像OmniFaces这样的实用程序库也是一个很好的补充。

Regarding the DAOs; 关于DAO; don't go as far as directly using the entity manager in (JSF) backing beans, but if you are already using Service classes (transactional boundaries) for your business logic , using a DAO as well might be overkill. 不要直接使用(JSF)支持bean中的实体管理器,但如果您已经在为业务逻辑使用Service类(事务边界),那么使用DAO可能会有点过分。

There are still some small advantages of a DAO, like a dao.findByName(...) looks a little clearer than loading a named query, setting a parameter, and getting a (single) result, but the cost is that you have to maintain a seperate DAO for each Entity, probably in addition to some Service. DAO还有一些小的优点,比如dao.findByName(...)看起来比加载命名查询,设置参数和获得(单个)结果更清晰,但成本是你必须的为每个实体维护一个单独的DAO,可能除了一些服务之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM