Challenge: Spring In-depth
Test your understanding of Spring annotations by refactoring an e-commerce application to adhere to the principles of IoC and DI.
We'll cover the following
Problem statement
Assume a hypothetical scenario where we have an e-commerce application for selling e-books that currently implements the following discount strategies:
Default discount: Applied to every purchase made online.
Bundle discount: Applied when a specific bundle of books is purchased.
Membership discount: Applied when a member purchases a book.
Although the application functions as intended, it has several significant issues:
The current implementation tightly manages dependencies within the application, leading to a rigid and less flexible structure violating the principles of Inversion of Control and Dependency Injection.
Dependencies are manually managed, increasing the risk of errors and making the system less maintainable.
The system's components are tightly coupled, reducing flexibility and making it harder to refactor.
The current design hampers the reusability and maintainability of components.
To address these issues, we need to refactor the application to adhere to the principles of IoC and DI.
Requirements
Your task is to convert this application into a Spring-based application that adheres to the principles of Inversion of Control (IoC) and Dependency Injection (DI). The project structure is already in place; you are required to configure the classes correctly using Spring annotations. Ensure the proper use of annotations such as @Component
, @Service
, @Autowired
, @Qualifier
, and @Primary
annotations where applicable.
Note: For this challenge, you need to configure a Spring application without using the Spring Boot framework.
Try it yourself
Use the following code widget to implement your solution.
Get hands-on with 1400+ tech skills courses.