Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaSpring Boot – Code Structure

Spring Boot – Code Structure

There is no specific layout or code structure for Spring Boot Projects. However, there are some best practices followed by developers that will help us too. You can divide your project into layers like service layer, entity layer, repository layer,, etc. You can also divide the project into modules. For example, the parent project has two child modules. The first module is for the data layer and the second module is for the web layer. You can also divide the project into features.

Note: Avoid Default Package

It is. because a class is said to be in a default package when it does not include a package declaration. It is not a best practice to include a class in the default package. It is because Spring scans the classes in packages and sub-packages mentioned in the annotations like @ComponentScan, @EntityScan, @SpringBootApplication etc. 

Note: It is recommended to use Java’s package naming conventions with a reverse domain name. For example, com.gfg.demo.

  • Main Application Class

It is recommended to place the Main Application class in the root package with annotations like @SpringBootApplication or @ComponentScan or @EnableAutoConfiguration. It allows the Spring to scan all classes in the root package and sub-packages. For example, if you are writing a JPA application similar to the below layout example, the MainApplicaiton.java is placed in the root package and all Customer-related classes in the sub-package com.gfg.demo.customer and Order related classes in the com.gfg.demo.order

Layout Structure is as follows:

Let us discuss two approaches that are typically used by most developers to structure their spring boot projects.

  1. Structure by Feature
  2. Structure by Layer

Structure 1: By feature

In this approach, all classes pertaining to a certain feature are placed in the same package. The structure by feature looks is shown in below example

Example

com
 +- gfg
     +- demo
         +- MyApplication.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

The advantages of this structure is as follows: 

  • Find a class to be modified is easy.
  • By deleting a particular sub-package, all the classes related to a certain feature can be deleted.
  • Testing and Refactoring is easy.
  • Features can be shipped separately.

Structure 2: By Layer

Another way to place the classes is by layer i.e; all controllers can be placed in controllers package and services under services package and all entities under domain or model etc.

Example

com
 +- gfg
     +- demo
         +- MyApplication.java
         |
         +- domain
         |   +- Customer.java
         |   +- Order.java
         |
         +- controllers
         |     +- OrderController.java
         |   +- CustomerController.java
         |
         +- services
         |    +- CustomerService.java
         |    +- OrderService.java
         |
         +- repositories
              +- CustomerRepository.java
              +- OrderRepository.java    

Though the above structure looks feasible and easy to locate classes by a layer. It has few disadvantages when compared to Structure by Feature. 

  • Features or Modules cannot be shipped separately.
  • Hard to locate a class pertaining to a certain feature.
  • Code Refactoring on a certain feature is difficult since the feature classes located in every layer.
  • It causes merge conflicts among developers using GitHub, BitBucket, etc. for Collaboration.

Now let us wrap up by acquiring the location of MainApplication.java. as in both the structures proposed above we have seen that the MainApplication.java is placed in the root package with @SpringBootApplication annotation. It s as shown below in as a sample example which is as follows:

Example

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}
RELATED ARTICLES

Most Popular

Recent Comments