📖 Scenario: You are building a Spring Boot application that has two modules: core for business logic and web for the web interface. This helps keep your code organized like departments in a company.
🎯 Goal: Create a multi-module Spring Boot project with a core module containing a service class and a web module that depends on core and exposes a REST endpoint.
📋 What You'll Learn
Create a parent pom.xml with modules core and web
In core module, create a service class GreetingService with a method getGreeting() returning "Hello from Core!"
In web module, create a Spring Boot application class WebApplication
In web module, create a REST controller GreetingController that uses GreetingService to return the greeting at /greet
💡 Why This Matters
🌍 Real World
Large Spring Boot applications often split code into modules to keep code organized and reusable, like departments in a company.
💼 Career
Understanding multi-module projects is important for working on enterprise Java applications and collaborating in teams.
Progress0 / 4 steps
1
Create parent pom.xml with modules
Create a parent pom.xml file that defines packaging as pom and includes two modules: core and web. Set the groupId to com.example and version to 1.0.0.
Spring Boot
Hint
The parent pom.xml should have packaging set to pom and list core and web as modules.
2
Create core module with GreetingService
In the core module, create a Spring Boot pom.xml with packaging jar and parent set to the root project. Then create a class GreetingService in package com.example.core with a method getGreeting() that returns the string "Hello from Core!".
Spring Boot
Hint
Make sure the core module pom.xml sets the parent and packaging correctly. The GreetingService class should have a method returning the exact string.
3
Create web module with Spring Boot application
In the web module, create a pom.xml with packaging jar, parent set to the root project, and a dependency on the core module. Then create a Spring Boot application class WebApplication in package com.example.web with the @SpringBootApplication annotation and a main method.
Spring Boot
Hint
Remember to add the core module as a dependency in the web module pom.xml. The WebApplication class needs the @SpringBootApplication annotation and a main method to start.
4
Create GreetingController using GreetingService
In the web module, create a REST controller class GreetingController in package com.example.web. Inject GreetingService from the core module using constructor injection. Add a @GetMapping for /greet that returns the greeting string from GreetingService.getGreeting().
Spring Boot
Hint
Use constructor injection to get GreetingService into GreetingController. Annotate the class with @RestController and add a method with @GetMapping("/greet") that returns the greeting.
Practice
(1/5)
1. What is the main purpose of a multi-module project in Spring Boot?
easy
A. To create multiple databases for one application
B. To run multiple Spring Boot applications simultaneously
C. To split a large project into smaller, manageable parts
D. To avoid using pom.xml files
Solution
Step 1: Understand project organization
Multi-module projects break a big codebase into smaller modules for easier management.
Step 2: Identify the main goal
This splitting helps teams work on parts independently and share code efficiently.
Final Answer:
To split a large project into smaller, manageable parts -> Option C
Quick Check:
Multi-module = smaller parts [OK]
Hint: Think of dividing work into smaller folders [OK]
Common Mistakes:
Confusing multi-module with running multiple apps
Thinking it removes the need for pom.xml
Assuming it creates multiple databases
2. Which file typically manages all modules in a Spring Boot multi-module project?
easy
A. application.properties
B. module-specific application.yml
C. settings.gradle
D. parent pom.xml
Solution
Step 1: Identify the build management file
In Maven-based Spring Boot projects, the parent pom.xml controls all modules.
Step 2: Understand file roles
Other files like application.properties configure app settings, not modules.
Final Answer:
parent pom.xml -> Option D
Quick Check:
Parent pom manages modules [OK]
Hint: Parent pom.xml controls modules [OK]
Common Mistakes:
Confusing application.properties with module management
Thinking settings.gradle is used in Maven projects
Assuming module-specific files manage all modules
3. Given a multi-module project with modules core and web, where web depends on core, what happens if core is not built before web?
medium
A. The web module build will fail due to missing dependencies
B. The web module will build successfully without core
C. Both modules will build independently without errors
D. The core module will build automatically after web
Solution
Step 1: Understand module dependencies
If web depends on core, it needs core built first to access its code.
Step 2: Consequence of build order
Building web without core causes missing dependency errors and build failure.
Final Answer:
The web module build will fail due to missing dependencies -> Option A
Quick Check:
Dependent module needs dependency built first [OK]
Hint: Build dependencies before dependents [OK]
Common Mistakes:
Assuming modules build independently
Thinking build order does not matter
Believing dependent modules build automatically
4. You have a multi-module Spring Boot project but the web module cannot find classes from the core module. What is the most likely cause?
medium
A. The core module is not listed as a dependency in web's pom.xml
B. The web module has no application.properties
C. The parent pom.xml is missing the modules section
D. The core module is using a different Java version
Solution
Step 1: Check module dependencies
If web cannot find core classes, it likely lacks a dependency declaration on core.
Step 2: Verify dependency declaration
Adding core as a dependency in web's pom.xml fixes class visibility.
Final Answer:
The core module is not listed as a dependency in web's pom.xml -> Option A
Quick Check:
Missing dependency causes class not found [OK]
Hint: Always declare module dependencies in pom.xml [OK]
Common Mistakes:
Ignoring dependency declarations
Thinking application.properties affects class visibility
Assuming Java version mismatch causes missing classes
5. In a multi-module Spring Boot project, you want to share common utility classes across modules without duplicating code. Which approach is best?
hard
A. Copy utility classes into each module's source folder
B. Create a separate common module and add it as a dependency to other modules
C. Put all utility classes in the parent pom.xml
D. Use application.properties to share utility classes
Solution
Step 1: Identify code sharing strategy
To avoid duplication, create a dedicated common module for shared utilities.
Step 2: Use module dependencies
Other modules add common as a dependency to access shared code cleanly.
Final Answer:
Create a separate common module and add it as a dependency to other modules -> Option B
Quick Check:
Shared code in common module [OK]
Hint: Use a common module for shared code [OK]
Common Mistakes:
Copying code causes duplication and maintenance issues
Trying to put code in pom.xml (not for code)
Using properties files to share classes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.