📖 Scenario: You are building a simple Spring Boot application for a small company. The company wants to keep the code clean and easy to maintain as it grows. You will learn why using enterprise patterns helps in organizing the code well.
🎯 Goal: Create a basic Spring Boot project with a simple data model, a configuration setting, a service layer using a common enterprise pattern, and a controller to complete the application structure.
📋 What You'll Learn
Create a data model class representing a product
Add a configuration property for product availability threshold
Implement a service class using the Service pattern to filter products
Create a REST controller to expose the filtered products
💡 Why This Matters
🌍 Real World
Enterprise patterns are used in real Spring Boot applications to keep code clean and maintainable as projects grow.
💼 Career
Understanding these patterns is essential for backend developers working with Spring Boot in professional environments.
Progress0 / 4 steps
1
Create the Product data model
Create a Java class called Product with two private fields: name of type String and price of type double. Include a constructor to set these fields and getter methods for both.
Spring Boot
Hint
Think of Product as a simple box holding a name and price. Use private fields and public getters.
2
Add a configuration property for availability threshold
Create a double variable called availabilityThreshold in a configuration class called AppConfig and set it to 50.0.
Spring Boot
Hint
Use a public static final variable in AppConfig to hold the threshold value.
3
Implement the ProductService using the Service pattern
Create a class called ProductService with a method filterAvailableProducts that takes a List<Product> and returns a List<Product> containing only products with price greater than AppConfig.availabilityThreshold. Use a for loop and an if statement.
Spring Boot
Hint
Use a list to collect products that pass the price check and return it.
4
Create a REST controller to expose available products
Create a Spring Boot REST controller class called ProductController with a method getAvailableProducts mapped to /products/available. This method should return the result of ProductService.filterAvailableProducts when given a list of three products: "Pen", 10.0, "Notebook", 60.0, and "Backpack", 80.0. Use @RestController and @GetMapping annotations.
Spring Boot
Hint
Use Spring annotations to create a REST endpoint that returns filtered products.
Practice
(1/5)
1. Why do enterprise patterns matter in Spring Boot applications?
easy
A. They remove the need for testing.
B. They make the application run faster by default.
C. They automatically generate user interfaces.
D. They organize large applications into clear, manageable parts.
Solution
Step 1: Understand the purpose of enterprise patterns
Enterprise patterns help organize complex applications into smaller, understandable parts.
Step 2: Relate to Spring Boot support
Spring Boot supports these patterns to make building and maintaining apps easier, not to speed up or auto-generate UI.
Final Answer:
They organize large applications into clear, manageable parts. -> Option D
Quick Check:
Enterprise patterns = organize apps [OK]
Hint: Think about how big projects stay organized [OK]
Common Mistakes:
Confusing patterns with performance improvements
Assuming patterns remove testing needs
Believing patterns auto-create UI
2. Which of the following is the correct way to define a Spring Boot service class following enterprise patterns?
easy
A. @Service public class UserService { }
B. public class UserService { }
C. @Controller public class UserService { }
D. @Repository public class UserService { }
Solution
Step 1: Identify the correct stereotype annotation for a service
In Spring Boot, @Service marks a service layer class following enterprise patterns.
Step 2: Check other annotations
@Controller is for web controllers, @Repository is for data access, and no annotation means no special role.
Final Answer:
@Service public class UserService { } -> Option A
Quick Check:
@Service = service layer class [OK]
Hint: Service classes use @Service annotation [OK]
Common Mistakes:
Using @Controller for service classes
Omitting stereotype annotations
Confusing @Repository with @Service
3. Given this Spring Boot code snippet, what will be the output when calling userService.getUserName(1) if the user exists?
@Service
public class UserService {
public String getUserName(int id) {
if (id == 1) return "Alice";
else return "Unknown";
}
}
medium
A. "Alice"
B. "Unknown"
C. null
D. Compilation error
Solution
Step 1: Analyze the method logic for id = 1
The method returns "Alice" if id equals 1.
Step 2: Confirm no errors or null returns
Since id is 1, it returns "Alice" directly, no errors or null.
Final Answer:
"Alice" -> Option A
Quick Check:
id=1 returns "Alice" [OK]
Hint: Check the if condition for the given id [OK]
Common Mistakes:
Assuming default return is null
Confusing else branch output
Thinking code has syntax errors
4. Identify the error in this Spring Boot repository interface following enterprise patterns:
public interface UserRepository {
User findById(int id);
}
medium
A. Missing @Repository annotation
B. User class must be final
C. Interface should extend JpaRepository or similar
D. Method name should be getUserById
Solution
Step 1: Check interface declaration for Spring Data JPA
Repository interfaces should extend JpaRepository or a similar interface to work properly.
Step 2: Verify other options
@Repository annotation is optional if extending JpaRepository; method name is fine; User class finality is unrelated.
Final Answer:
Interface should extend JpaRepository or similar -> Option C
Quick Check:
Repository interface must extend JpaRepository [OK]
Hint: Repositories extend JpaRepository for Spring Data support [OK]
Common Mistakes:
Forgetting to extend JpaRepository
Thinking @Repository is always required
Changing method names unnecessarily
5. You want to build a Spring Boot app that is easy to maintain and test. Which enterprise pattern combination helps achieve this best?
hard
A. Put all logic in one class annotated with @SpringBootApplication
B. Use layered architecture with @Controller, @Service, and @Repository layers
C. Use only @RestController and skip service layer
D. Write all database queries directly in controller methods
Solution
Step 1: Understand layered architecture benefits
Separating concerns into controller, service, and repository layers makes code easier to maintain and test.
Step 2: Evaluate other options
Putting all logic in one class or skipping layers leads to messy, hard-to-test code.
Final Answer:
Use layered architecture with @Controller, @Service, and @Repository layers -> Option B