Training Guide ‐ Spring AOP - vinhtbkit/bkit-kb GitHub Wiki

Spring AOP

Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

Spring AOP is a feature of the Spring Framework that allows developers to define certain behaviors (i.e., “aspects”) that cut across multiple classes, such as logging or transaction management. These behaviors, which are called “advices,” can be applied to specific “join points” (i.e., points in the execution of a program) in the application, using “pointcuts” to determine where the advices should be applied.

Target: the trainee will know how to use the Spring AOP and how it works.
Expected Duration: maximum 48 hours of training and working on exercises.

Qualification criteria:

  • The trainee must understand the AOP concepts.
  • Know how to use the Spring AOP in @AspectJ annotation style.
  • Know how the Spring AOP works.
  • Difference between Spring AOP and AspectJ.

AOP Concepts REQUIRED

  • What do these terms indicate?: Aspect, Joinpoint, Advice, Pointcut, Target object(Advised Object)
  • How many advice types that the Spring AOP have and their responsibilities?

References:

Pointcut Designators

  • At least understand how to use: execution, args, @annotation REQUIRED

References:

Apply the Spring AOP by using @AspectJ annotation style REQUIRED

References:

Using the Spring AOP by using Schema-based AOP Support OPTIONAL

References:

Proxying Mechanisms REQUIRED

References:

Programmatic Creation of @AspectJ Proxies OPTIONAL

References:

Using AspectJ with Spring Applications OPTIONAL

References:

Quizzes

  • Drawbacks of the Spring AOP?
  • Difference between AspectJ and Spring AOP?
  • Can I advise a final, private, protected, or static method by Spring AOP? Why? How?
  • Can I advise an object that is outside the Spring IoC? Why? How?
  • Does Spring AOP work when the method is invoked by another method in the same class? (internal calling) Explain.

Assignments

Overview

  • Using Spring Boot, Spring AOP (using AspectJ annotation style)

Requirement Details:
Assume that we have a REST controller that manages the products in your application:

  • The controller should manage the CRUD of the products. (Ex: get a product, get products, create a product, update a product, delete a product)
  • Write your own simple Controller Advice (Advice handling all exceptions thrown by the product controller). Example: when the product is not found throw a 404 error with a description.
  • The CRD endpoints (like creating a product, updating a product, or deleting a product) only allow the IP addresses in the whitelist (defined by yourself) to execute. (Using Spring AOP to handle this cross-cutting concern).
  • Create an aspect that can help the developer check the time execution of all product endpoints when the log level is DEBUG.
  • You can select any kinds of database storage, or even storing in-memory with any data structure (Map or List ....)