Spring Aspect - Tuong-Nguyen/Spring GitHub Wiki

AOP - Aspect-Oriented Programming

Promote separation of concerns

Systems are composed of several components

  • Components for specific functionality
  • Additional responsibility beyond the core functionality, such as: logging, transaction management, security

2 problems solved by the AOP:

  • code tangling: there are many code which does not relate to main code; example: UserRepository.getUsers method may contain code for logging, checking if user has permission ...
  • code scattering: the same code appears in many places; example: logging code appears in UserRepository.getUsers, OrderRepository.getOrderById ...

System-wide concerns

System-wide concerns

Cross-cutting concerns

With AOP, we can then cover our core application with layers of functionality. These layers can be applied declaratively throughout our application in a flexible manner without our core application even knowing they exist

Cross-cutting concerns

How Spring implements AOP

@EnableAspectJAutoProxy //Enable AOP
public class AppConfig{
}

Proxy class is generated which wraps the used class.

Spring limitation

AOP is applied for public methods of Spring managed beans.

Terminologies

Join Cross Join Point Advice @Before @After @Throw @Finally @Around