Objective
Understand Aspect-Oriented Programming (AOP) and its implementation in the Spring Framework to address cross-cutting concerns such as logging. Learn AOP terminology, annotations, and how to apply Spring AOP to log HTTP requests in a Spring MVC application, specifically enhancing the Library Management System from previous sessions.
Lecture Topics
1. AOP Overview
- Definition: Aspect-Oriented Programming (AOP) is a programming paradigm that modularizes cross-cutting concerns—functionalities like logging, security, or transaction management that span multiple parts of an application.
- Cross-Cutting Concerns: Common tasks (e.g., logging method calls, authentication) that are scattered across multiple classes, leading to code duplication if not handled separately.
- Purpose:
- Separates business logic from cross-cutting concerns to improve modularity.
- Reduces boilerplate code by centralizing concern logic (e.g., logging in one place).
- Enhances maintainability and testability.
- Use Cases:
- Logging method executions and parameters.
- Enforcing security checks before method execution.
- Managing database transactions.
- Monitoring performance (e.g., execution time).
- AOP vs. OOP:
- OOP organizes code into classes for business logic.
- AOP handles concerns that cut across multiple classes, complementing OOP.
- Exam Tip: Be able to define AOP and list examples of cross-cutting concerns.
2. Spring AOP
-
Definition: Spring AOP is Spring’s implementation of AOP, using proxy-based weaving to apply aspects at runtime. It leverages AspectJ annotations but does not require the full AspectJ compiler.
-
Key Features:
- Proxy-Based: Uses JDK dynamic proxies (for interfaces) or CGLIB (for classes) to wrap target objects with aspect logic.
- Integration with IoC: Aspects are managed as Spring beans, making them easy to configure and inject.
- Runtime Weaving: Applies aspects during application execution, simpler than AspectJ’s compile-time weaving.
-
Limitations:
- Supports only method-level join points (not fields or constructors).
- Advises only Spring-managed beans by default.
- Less powerful than full AspectJ (e.g., cannot handle complex pointcuts like field access).
-
Comparison with AspectJ:
- Spring AOP: Lightweight, proxy-based, limited to Spring beans, easier to use.
- AspectJ: Full-featured, supports compile-time/load-time weaving, more complex.
-
Dependencies:
-
Exam Tip: Understand the proxy-based mechanism and limitations of Spring AOP.
3. AOP Terminology and Annotations
- Key Terminology:
- Aspect: A module combining pointcuts and advice, encapsulating a cross-cutting concern (e.g., a logging class).
- Marked with
@Aspect
and @Component
in Spring.
- Join Point: A specific point in program execution where aspect code can be applied (e.g., method execution, exception handling).
- In Spring AOP, join points are limited to method executions.
- Pointcut: A predicate that selects join points where advice should be applied (e.g., methods in a specific package).
- Defined using expressions like
execution(* com.example.*.*(..))
.
- Advice: The code executed at a join point (e.g., logging before a method).
- Types: Before, After, AfterReturning, AfterThrowing, Around.
- Weaving: The process of applying aspects to target objects (runtime weaving in Spring AOP).
- Target Object: The object being advised (e.g., a controller or service).
- Proxy: A wrapper object created by Spring to apply aspect logic.