There comes a time while the development phase of an application when some things get complex. It becomes critically hard for managing the services, repositories, entities, etc of an application. As we know that the Spring Boot was developed for the reasons like auto-configuration, efficiency, quick development, reduce cumbersome efforts, etc. To overcome this or a similar issue, we can use Spring Boot to make the application as the Multi-Module Project. In a Multi-Module Project, an application is divided into multiple modules, where each module plays an important role in the certain functionality of an application. A module can be considered as an independent project or sub-project.
Note: Multi-Module Project is just a set of multiple projects where each project has its own respective function.
Advantages of a Multi-Module Project
- It provides a great ability to build all sub-modules only with a single command.
- We can run the build command from the parent module.
- While building the application, the build system takes care of the build order.
- Deployment of the applications gets very convenient and flexible.
- Also, the code from the various modules across different projects can be re-used.
- Last but not the least, with the help of Multi-Module Project architecture, we can gain benefits from the microservices architecture.
Working of the Multi-Module Project
- Firstly you have to create a Parent module.
- The parent module acts as a container of sub-modules.
- Also, you can use the Parent module for bootstrapping of the application ( main() method ).
- Alternatively, you can also use any of the sub-module by implementing the main() method for bootstrapping the application and deleting the main() method from the parent module.
- Your Parent module should have ‘pom’ packaging instead of jar and war.
- When you create sub-modules, all of them get listed in the ‘<modules>’ tag in pom.xml of the parent module.
- All the dependencies you have enlisted in the pom.xml of the parent module will be automatically inherited by sub-modules directly.
- Therefore, you don’t need to add dependencies to sub-module’s pom.xml to use them.
Creating Multi-Module Project (STS – Spring Tool Suite)
- Create a simple Spring Starter Project ( File -> New -> Spring Starter Project -> Next -> ( Select Dependencies ) -> Next -> Finish )
- Change or add ‘<packaging>pom</packaging>’ .
- Add a sub-module ( Right click on Parent Module -> New -> other -> Maven -> Maven Module -> Next ( Select both checkboxes ) -> Next -> jar or war -> Finish ).
- After creating sub-modules, the parent module will contain the folders of sub-modules, and also independent projects of the already created respective sub-modules will be created.
ParentGFG – Parent Module
pom.xml (Configurations)
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.6.3</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >sia</ groupId > < artifactId >ParentGFG</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >pom</ packaging > < name >ParentGFG</ name > < description >Multi Module Project</ description > < properties > < java.version >11</ java.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-thymeleaf</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-devtools</ artifactId > < scope >runtime</ scope > < optional >true</ optional > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < excludes > < exclude > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > </ exclude > </ excludes > </ configuration > </ plugin > </ plugins > </ build > < modules > < module >GFG-Module1</ module > < module >GFG-Module2</ module > </ modules > </ project > |
Notes:
- After creating Spring Starter Project with a jar or war packaging, make sure that you add or change the ‘packaging’ tag with ‘pom’ -> ‘<packaging>pom</packaging>’.
- When you will create sub-modules, the ‘modules’ tag will automatically get generated with added respective modules.
- You can also remove the ‘src’ folder from Parent Module after implementing the main() method in any of the sub-module, as the Parent Module can also act just like a container.
GFG-Module1 (Sub-Module)
pom.xml (Configurations)
- Declares a parent module of which this sub-module will be a part.
- Information tags of this sub-module are also initialized as well.
XML
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < parent > < groupId >sia</ groupId > < artifactId >ParentGFG</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ parent > < artifactId >GFG-Module1</ artifactId > < name >GFG-Module1</ name > < description >Lazyroar</ description > </ project > |
Main.java (Bootstrapping of the Application)
Java
package controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Main { public static void main(String[] args) { SpringApplication.run(Main. class , args); } } |
RestApi.java (Endpoint of the Application)
- This class uses ‘Starter Web’ dependency which is automatically inherited from the parent module’s pom.xml.
- This controller class accepts HTTP requests.
- Here, the HTTP GET request is accepted by get() method.
- This method uses an entity class that is created and declared in a sub-module ( GFG-Module2 ).
- To import classes etc from different modules, you have to hover on the red error underline.
- Click -> ‘fix project setup’ and select the respective sub-module where you have created the class in.
Java
package controller; import entity.UserModel; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping ( "/get" ) public class RestApi { @GetMapping public UserModel get() { UserModel entity = new UserModel(); entity.setId( "1" ); entity.setName( "Darshan.G.Pawar" ); entity.setEmail( "geek@geek" ); entity.setPincode( "422 009" ); return entity; } } |
GFG-Module2 ( Sub-Module )
pom.xml (Configurations)
XML
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < parent > < groupId >sia</ groupId > < artifactId >ParentGFG</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ parent > < artifactId >GFG-Module2</ artifactId > < name >GFG-Module2</ name > < description >Lazyroar</ description > </ project > |
UserModel.java (User data entity class)
- This class represents a user entity class, which has been used by the get() method of the RestApi.java class.
- This class requires an important set of Getter and Setter methods of all instance variables fields.
- This class uses ‘Lombok’ dependency which is automatically inherited from the parent module’s pom.xml.
- Therefore, the ‘@Data‘ annotation of the Lombok library is used to automatically generate Getter and Setter methods at runtime.
- The annotation ‘@RequiredArgsConstructor‘ is used to automatically generate a constructor for required ( where the constrain is ‘@NonNull’ ) or final fields of a class.
- If a class doesn’t contain fields, then the ‘@RequiredArgsConstructor‘ annotation acts as a ‘@NoArgsConstructor‘ annotation which creates zero parameter constructor.
Java
package entity; import lombok.Data; import lombok.RequiredArgsConstructor; @Data @RequiredArgsConstructor public class UserModel { String id; String name; String email; String pincode; } |
Output: RestApi.java of GFG-Module1
- GFG-Module2 (sub-module) declares an entity (Data) object.
- This data is used in the body of the response which is returned by the RESTful API of GFG-Module1 ( sub-module ).
Note: You will have to run the GFG-Module1 as the main() method and the controller class is in this sub-module.