Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Advisor and Aspect in AOP?

Tags:

java

spring

aop

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?

like image 492
Ruolin Avatar asked Aug 02 '14 06:08

Ruolin


People also ask

What is advisor in AOP?

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.

What is an aspect and pointcut in AOP?

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.

What is aspect in AOP Spring?

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.

What is the difference between JoinPoint and pointcut?

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.


2 Answers

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.

like image 157
kriegaex Avatar answered Oct 24 '22 17:10

kriegaex


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

enter image description hereenter image description here

like image 23
Braj Avatar answered Oct 24 '22 17:10

Braj