I am new to Spring AOP. Based on my understanding, I noticed that both Advisor (for example DefaultPointcutAdvisor
) and Aspect (for example the class annotated with @Aspect
) can both help to solve the cross-cutting problem by doing something more when a method is invoked.
What is the different between these two term please?
Interface Advisor Spring AOP is based around around advice delivered via method interception, compliant with the AOP Alliance interception API. The Advisor interface allows support for different types of advice, such as before and after advice, which need not be implemented using interception.
It is the action taken by an aspect at a particular join-point. Joinpoint is a point of execution of the program, such as executing a method or handling an exception. In Spring AOP, a joinpoint always represents a method execution. Pointcut is a predicate or expression that matches join points.
Aspect: An aspect is a class that implements enterprise application concerns that cut across multiple classes, such as transaction management. Aspects can be a normal class configured through Spring XML configuration or we can use Spring AspectJ integration to define a class as Aspect using @Aspect annotation.
JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.
Advisors seem to be an old "AOP lite" type of defining cross-cutting concerns from Spring 1.2 when Java 5 usage was still somewhat uncommon and thus @AspectJ syntax (via Java annotations) not used in Spring. The concept has still survived for lovers of schema-based AOP rather than annotation-based AOP or pure AspectJ syntax, see Spring documentation on advisors.
The concept of "advisors" comes from the AOP support defined in Spring and does not have a direct equivalent in AspectJ. An advisor is like a small self-contained aspect that has a single piece of advice. The advice itself is represented by a bean and must implement one of the advice interfaces described in Advice Types in Spring. Advisors can take advantage of AspectJ pointcut expressions.
Most aspects are a combination of advice that defines the aspect’s behavior and a pointcut defining where the aspect should be executed.
Spring recognizes this and offers advisors, which combine advice and pointcuts into one object.
More specifically, the PointcutAdvisor
does this.
public interface PointcutAdvisor {
Pointcut getPointcut();
Advice getAdvice();
}
Most of Spring’s built-in pointcuts also have a corresponding PointcutAdvisor
.
This is convenient if you want to define a pointcut and the advice it is managing
in one place.
Read more in Spring in Action, 3rd Edition
Sanpshots
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With