Spring Boot – Multi-Module Project
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
- 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.
- Declares a parent module of which this sub-module will be a part.
- Information tags of this sub-module are also initialized as well.
Main.java (Bootstrapping of the Application)
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.
GFG-Module2 ( Sub-Module )
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.
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.