Module 6: Advanced Backend Frameworks (Java)
This module explores advanced capabilities of backend frameworks such as Spring Boot and Spring MVC. Students will learn about dependency injection, middleware, data binding, and layered application architecture.
6.1 Learning Objectives
- Understand the structure of a Spring Boot application.
- Implement controllers, services, and repositories following MVC architecture.
- Use dependency injection to improve modularity and testability.
- Implement middleware (filters and interceptors) for request handling.
6.2 Application Architecture (MVC)
- Model: Represents the data and business logic.
- View: Defines how data is presented (Thymeleaf, JSON).
- Controller: Manages the interaction between model and view.
6.3 Dependency Injection Example
@Service
public class UserService {
public String getUser() {
return "John Doe";
}
}
@RestController
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/user")
public String getUser() {
return userService.getUser();
}
}
6.4 Middleware (Filters and Interceptors)
@Component
public class RequestLoggingFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
throws ServletException, IOException {
System.out.println("Request URI: " + request.getRequestURI());
filterChain.doFilter(request, response);
}
}
6.5 Exercise
- Build a multi-layered application using Spring MVC.
- Implement a service and repository layer for a “Product” entity.
- Add a request logging filter to monitor API usage.