java EAR 与单独的 EJB + WAR

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

EAR vs separate EJB + WAR

javajakarta-eewarjava-ee-6ear

提问by karolkpl

What's the difference in deploying application as EAR (with 1 EJB and 1 WAR module) vs separate modules? I want to use GlassFish 3 web profile but it does not support EAR archive. Can I simply use EJB and WAR as separate modules? Any other options?

将应用程序部署为 EAR(带有 1 个 EJB 和 1 个 WAR 模块)与单独的模块有什么区别?我想使用 GlassFish 3 Web 配置文件,但它不支持 EAR 存档。我可以简单地将 EJB 和 WAR 用作单独的模块吗?还有其他选择吗?

回答by Arjan Tijms

There seems to be some confusion between 3 variants of deployment:

3 种部署变体之间似乎有些混淆:

  1. An EAR that includes an EJB and WEB module
  2. Deploying a separate EJB module and a separate WEB module
  3. Deploying a WEB module that includes EJB classes or an EJB jar.
  1. 包含 EJB 和 WEB 模块的 EAR
  2. 部署单独的 EJB 模块和单独的 WEB 模块
  3. 部署包含 EJB 类或 EJB jar 的 WEB 模块。

In the first situation, you have logically one application, but one that is divided in two tiers. The WEB module is isolated from the EJB module in the sense that it can consume classes from EJB module, but the EJB module can not consume classes from the WEB module. Since it's a single application local access to EJB beans can be used and injection of EJB beans works as expected.

在第一种情况下,您在逻辑上有一个应用程序,但一个应用程序分为两层。WEB 模块与EJB 模块隔离,它可以消费来自EJB 模块的类,但EJB 模块不能消费来自WEB 模块的类。由于它是单个应用程序,因此可以使用对 EJB bean 的本地访问,并且 EJB bean 的注入按预期工作。

In the second situation (which you seem to be referring to in your question) there isn't a logical single application, but really two separate modules. They do run in the same JVM, but officially Java EE does not allow to use local access and remote access has to be used (although practically local access often works anyway). Also, injection of EJB beans in beans in the web module does not work directly with a simple @EJBannotation, but instead the lookupattribute has to be used that specifies the global JNDI name.

在第二种情况下(您似乎在问题中提到了这种情况),没有逻辑上的单个应用程序,而是实际上有两个独立的模块。它们确实在同一个 JVM 中运行,但官方 Java EE 不允许使用本地访问,必须使用远程访问(尽管实际上本地访问通常仍然有效)。此外,在 Web 模块的 bean 中注入 EJB bean 不能直接使用简单的@EJB注释,而是lookup必须使用指定全局 JNDI 名称的属性。

Finally, the third situation (which you don't seem to mention, but 'home' mentions) is a bit similar to the first one, but there are no tiers and isolation in this case. EJB beans can access all classes from the rest of the web module directly.

最后,第三种情况(您似乎没有提到,但“家”提到了)与第一种情况有点相似,但在这种情况下没有层级和隔离。EJB bean 可以直接从 Web 模块的其余部分访问所有类。

The web profile only supports this last deployment situation. Both EAR and standalone EJB deployments are not supported.

Web 配置文件仅支持最后一种部署情况。EAR 和独立 EJB 部署均不受支持。

回答by home

Whats the difference in deploying application as ear (with 1 ejb and 1 war module) vs separate modules?

将应用程序部署为 ear(带有 1 个 ejb 和 1 个 war 模块)与单独的模块有什么区别?

Not a complete list: In an EAR you can define Utility JARsas well, they are located in e.g. EAR/liband can be reused by WARsand EJB JARs. Often EAR files provide prorietary deployment features, e.g. in WebSphere you can specify DataSource details, so that you do not have to define a DataSource (and JDBC driver) using management utilitiers.

不是一个完整的列表:在 EAR 中,您也可以定义Utility JAR,它们位于例如EAR/lib 中,并且可以被WAREJB JAR重用。EAR 文件通常提供专有的部署功能,例如在 WebSphere 中,您可以指定数据源的详细信息,这样您就不必使用管理实用程序定义数据源(和 JDBC 驱动程序)。

Since I want use Glassfish 3 web profile, but it does not support ear archive. Can I simply use ejb and war as separate modules? Any other options?

因为我想使用 Glassfish 3 网络配置文件,但它不支持耳朵存档。我可以简单地使用 ejb 和 war 作为单独的模块吗?还有其他选择吗?

Yes, the web profile spec explicitly allows you to deploy a lightweightEJB version as part of the WAR. Simply place the EJB JARinside the WARsWEB-INF/lib. This link provides a comparison of features (Web profile vs full-blown): http://glassfish.java.net/downloads/v3-final.html

是的,Web 配置文件规范明确允许您部署轻量级EJB 版本作为WAR 的一部分。只需将EJB JAR放在WAR 中WEB-INF/lib。此链接提供了功能比较(Web 配置文件与完整功能):http: //glassfish.java.net/downloads/v3-final.html