简体   繁体   中英

How to setup a Java EE6 application structure

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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