Skip to content

Instantly share code, notes, and snippets.

@cassiusvm
Last active December 28, 2018 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassiusvm/c5eca8a774275812bb055f07c7f9a934 to your computer and use it in GitHub Desktop.
Save cassiusvm/c5eca8a774275812bb055f07c7f9a934 to your computer and use it in GitHub Desktop.
Spring Boot Package Layout

Please, see this: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html

Ok, it's not clear we should keep the same package structure with multi-modules maven project.

Spring Boot Reference Guide suggest us a typical layout:

com
 +- example
     +- myapplication
         +- Application.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

The package structure is:

com.example.myapplication.Application
com.example.myapplication.customer.Customer
com.example.myapplication.customer.CustomerController
com.example.myapplication.customer.CustomerService
com.example.myapplication.customer.CustomerRepository
com.example.myapplication.order.Order
com.example.myapplication.order.OrderController
com.example.myapplication.order.OrderService
com.example.myapplication.order.OrderRepository

But we generally do:

.
└── com
    └── example
        └── myapplication
            ├── Application.java
            ├── controllers
            │   ├── CustomerController.java
            │   └── OrderController.java
            ├── model
            │   ├── Customer.java
            │   └── Order.java
            ├── repositories
            │   ├── CustomerRepository.java
            │   └── OrderRepository.java
            └── services
                ├── CustomerService.java
                └── OrderService.java

Then the package structure is:

com.example.myapplication.Application
com.example.myapplication.controllers.CustomerController
com.example.myapplication.controllers.OrderController
com.example.myapplication.model.Customer
com.example.myapplication.model.Order
com.example.myapplication.repositories.CustomerRepository
com.example.myapplication.repositories.OrderRepository
com.example.myapplication.services.CustomerService
com.example.myapplication.services.OrderService

If you are using multi-module maven project then keep the same layout:

.
├── module-data
│   └── src
│       └── main
│           └── java
│               └── com
│                   └── example
│                       └── myapplication
│                           ├── model
│                           │   ├── Customer.java
│                           │   └── Order.java
│                           ├── repositories
│                           │   ├── CustomerRepository.java
│                           │   └── OrderRepository.java
│                           └── services
│                               ├── CustomerService.java
│                               └── OrderService.java
└── module-web
    └── src
        └── main
            └── java
                └── com
                    └── example
                        └── myapplication
                            ├── Application.java
                            └── controllers
                                ├── CustomerController.java
                                └── OrderController.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment