What is the difference between an adviser and an aspect in AOP?

I am new to Spring AOP. Based on my understanding, I noticed that both Advisor (e.g. DefaultPointcutAdvisor ) and Aspect (e.g. a class annotated using @Aspect ) can help solve the problem of cross-cutting tasks by doing something else when calling the method.

What is the difference between these two terms?

+7
java spring aop
source share
4 answers

Advice is the way you perform an action on a Pointcut . You can use before, after or even around the board to apply any actions that you have identified. Speaking of Spring Aspect , this is only a class that is high level and combines two concepts: jointpoint and Advice . This can be done using an XML plan or programmatically. You must also specify your point at which you want to connect the aspect, this is done using jointpoint .

Also, Spring Aspects and Advice do not replace each other, because Aspects are just a merger for a common point and advice.

+1
source share

Most aspects are a combination of tips that define aspects of behavior and a pointcut that determine where the aspect should be performed.

Spring recognizes this and offers advisors that combine tips and pointcuts into one object.

More specifically, PointcutAdvisor does this.

 public interface PointcutAdvisor { Pointcut getPointcut(); Advice getAdvice(); } 

Most Spring s built-in pointcuts also have a corresponding PointcutAdvisor . This is useful if you want to define a pointcut and the advice it manages in one place.

More in Spring in Action, 3rd Edition

Sanpshots

enter image description hereenter image description here

+6
source share

Advisers seem to be old AOP lite types for identifying cross-cutting issues from Spring 1.2, when using Java 5 was still somewhat unusual, and therefore the @AspectJ syntax (via Java annotations) was not used in Spring. The concept is still valid for AOP lovers based on diagrams rather than AOP or AspectJ syntax based on annotations, see Spring Advisor documentation .

+1
source share

In my understanding, the Aspect is only an aspect of oriented programming, and the adviser is Spring Framework jargon.

Perhaps this simple example will be useful:

Foo.java

 public interface Foo { void foo(); void baz(); } 

FooImpl.java

 public class FooImpl implements Foo { @Override public void foo() { System.out.println("Foo!"); } @Override public void baz() { System.out.println("Baz!"); } } 

MethodBeforeAdviceBarImpl.java

 import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class MethodBeforeAdviceBarImpl implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("Bar!"); } } 

and finally App.java

 import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.NameMatchMethodPointcutAdvisor; public class App { public static void main(String[] args) { final MethodBeforeAdvice advice = new MethodBeforeAdviceBarImpl(); final NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor = new NameMatchMethodPointcutAdvisor(); nameMatchMethodPointcutAdvisor.setMappedName("foo"); nameMatchMethodPointcutAdvisor.setAdvice(advice); final ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(nameMatchMethodPointcutAdvisor); final Foo foo = new FooImpl(); proxyFactory.setTarget(foo); final Foo fooProxy = (Foo) proxyFactory.getProxy(); fooProxy.foo(); fooProxy.baz(); } } 

Running the main method in App.java outputs:

 Bar! Foo! Baz! 

So, as you see here, NameMatchMethodPointcutcutAdvisor is an Expert Advisor , and it consists of a mappedName, which is a pointcut and the advice itself, which in this case is MethodBeforeAdvice .

And in Jargon's aspect oriented programming, Aspect is Advice + Pointcut, so you go. Advisor seems to be Aspect in the end ..

0
source share

All Articles