如何设置 Java EE6 应用程序结构

发布于 2025-01-07 01:44:36 字数 492 浏览 1 评论 0原文

因此,在阅读了 DDD 及其所有好处和荣耀之后,Java EE 似乎并不能让您轻松做到这一点。我的想法是制作这样的结构:

Domain
存储库
应用
然而

,在这个答案的评论中DDD和应用程序层似乎是应用程序 层,我认为将成为所有用 @Stateful、@WebService 等注释的服务的层,但这并不是它真正应该位于的位置。域模型似乎应该具有这些注释。

所以现在的问题是:人们如何构建他们的应用程序?您将不同的注释放在哪里以及它们如何相互使用。有人可以帮助我了解如何构建 java ee 6 Web 应用程序吗?请帮助并说明我如何在特定工具或类似工具中执行此操作,而是说明实际类的位置以及不同层的用途。

我对从哪里开始以及如何组织感到沮丧。

So after reading about DDD and all it benefits and glory it seems like Java EE does not make it easy for you to do so. What I thought was to make a structure like this:

Domain
Repository
Application
View

However in the comment of this answer DDD and application layer it seems like the Application layer which I thought was going to be the layer with all the services annotated with @Stateful, @WebService etc is not the place it really should be in. It seems like the domain models should have these annotations.

So now the question is: How do people structure their applications? Where do you put the different annotations and how do they use each other. Could somebody please help me understand how I can structure an java ee 6 web application? Please help and say not how I do it in a specific tool or anything like that but where the actual classes goes and what the different layers are intended to do.

I am frustrated on where to start and how to organize.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2025-01-14 01:44:36

对于如何构建您的应用程序,没有具体的规则。最好是运用常识并观察其他人是如何做的。

您可以生成一个由焊接团队提供的简单 Maven 项目,以了解如何构建基本的 Java EE 应用程序:

mvn archetype:generate -DarchetypeArtifactId=jboss-javaee6-webapp -DarchetypeGroupId=org.jboss.weld.archetypes -DarchetypeVersion=1.0.1.CR1 -DarchetypeRepository=central

当然,您会在 github 或 java.net 上找到许多其他示例

There are no specific rules on how you should structure your application. Best would be to use common sense as well as observe how others are doing it.

You can generate a simple maven project provided by weld team to see how a basic Java EE application can be structured:

mvn archetype:generate -DarchetypeArtifactId=jboss-javaee6-webapp -DarchetypeGroupId=org.jboss.weld.archetypes -DarchetypeVersion=1.0.1.CR1 -DarchetypeRepository=central

For sure you will find many other examples on github or java.net

屋顶上的小猫咪 2025-01-14 01:44:36

这是一个可能有用的示例 -> EAR 测试

它被称为“EAR 测试”,但也可以像轻松应用于构建战争文件。为了这个答案的目的,我将示例中提到的 eartesting 目录更改为 wartesting

EAR 文件和 WAR 文件几乎相同,因为在 Java EE 规范级别我们决定允许 war 文件包含 EJB、CDI bean 等。

该示例使用 Maven 构建系统并具有两个模块,一个用于“数据对象”,一个用于“业务逻辑”。似乎符合您的想法,并且可能是一个有用的起点。它包含一个小型示例应用程序,其中包含 EJB 的单元测试。

您可能还没有读过,但人们经常认为 EJB 难以测试。现在已经没有了,该示例显示了最新的符合规范的解决方案,因此您可以从该设置开始用一块石头杀死几只鸟。

其中不包括用于创建将在生产中部署的最终 WAR 文件的模块。要创建它,您只需

  • 添加第三个模块wartesting/business-model
  • wartesting/business-logic
  • wartesting/business-war (已添加)

business-war 中 d 有一个如下所示的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <parent>
    <groupId>org.superbiz</groupId>
    <artifactId>myear</artifactId>
    <version>1.1-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <artifactId>business-war</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.superbiz</groupId>
      <artifactId>business-model</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.superbiz</groupId>
      <artifactId>business-logic</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

然后创建以下目录:

  • wartesting/business-war/src/main/java
  • wartesting/business-war/src/main/webapp

我们会说例如你添加每个以下文件:

  • wartesting/business-war/src/main/java/org/superbiz/Foo.java
  • wartesting/business-war/src/main/webapp/WEB-INF/web.xml
  • wartesting/business-war/src /main/webapp/index.html

构建后,您应该在 wartesting/business-war/target/ 下获得一个 war 文件,其中包含:

  • WEB-INF/web.xml
  • WEB-INF/classes/org/superbiz/Foo.class
  • WEB-INF/lib/business-model-1.1-SNAPSHOT.jar
  • index.html

Here's an example that might be helpful -> EAR Testing

It's called "EAR Testing", but can just as easily apply to building war files. For purposes of this answer I'll change the eartesting directory mentioned in the example to wartesting

EAR files and WAR files are nearly identical since at the Java EE spec level we decided to allow war files to contain EJBs, CDI beans, and more.

That example uses the Maven build system and has two modules, one for the "data obects" and one for "business logic". Seems to fit with how you think of thinks and might be a helpful starting point. It contains a tiny sample application with unit tests for the EJBs.

You might not have read yet, but often people refer to EJBs as hard to test. There're not anymore and that example shows the latest spec compliant solution, so you can kill a few birds with one stone starting from that setup.

What that doesn't include is a module to create the final WAR file that you would deploy in production. To create that you'd just add a third module

  • wartesting/business-model
  • wartesting/business-logic
  • wartesting/business-war (added)

In the busines-war you'd have a pom.xml like the following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <parent>
    <groupId>org.superbiz</groupId>
    <artifactId>myear</artifactId>
    <version>1.1-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <artifactId>business-war</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.superbiz</groupId>
      <artifactId>business-model</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.superbiz</groupId>
      <artifactId>business-logic</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Then create the following directories:

  • wartesting/business-war/src/main/java
  • wartesting/business-war/src/main/webapp

And we'll say for example you add the following files to each:

  • wartesting/business-war/src/main/java/org/superbiz/Foo.java
  • wartesting/business-war/src/main/webapp/WEB-INF/web.xml
  • wartesting/business-war/src/main/webapp/index.html

Once built, you should get a war file under wartesting/business-war/target/ containing:

  • WEB-INF/web.xml
  • WEB-INF/classes/org/superbiz/Foo.class
  • WEB-INF/lib/business-model-1.1-SNAPSHOT.jar
  • index.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文