关于Java:AOP中Advisor和Aspect之间有什么区别?

What is the difference between Advisor and Aspect in AOP?

我是Spring AOP的新手。 根据我的理解,我注意到Advisor(例如DefaultPointcutAdvisor)和Aspect(例如带有@Aspect注释的类)都可以通过在调用方法时做更多的事情来帮助解决横切问题。

这两个词有什么区别?


大多数方面是结合建议的,这些建议定义了
方面的行为以及定义方面应在何处执行的切入点。

Spring意识到了这一点,并提供了结合了建议和切入点的顾问
变成一个对象

更具体地说,PointcutAdvisor会执行此操作。

1
2
3
4
public interface PointcutAdvisor {
   Pointcut getPointcut();
   Advice getAdvice();
}

Spring的大多数内置切入点也都有相应的PointcutAdvisor
如果要定义切入点及其管理的建议,这很方便
在一个地方。

在第三版的《 Spring in Action》中阅读更多内容

Sanpshots

enter image description here
enter image description here


以我的理解,Aspect只是面向方面的编程术语,而Advisor是Spring Framework术语。

也许这个简单的例子会有所帮助:

Foo.java

1
2
3
4
public interface Foo {
    void foo();
    void baz();
}

FooImpl.java

1
2
3
4
5
6
7
8
9
10
11
public class FooImpl implements Foo {
    @Override
    public void foo() {
        System.out.println("Foo!");
    }

    @Override
    public void baz() {
        System.out.println("Baz!");
    }
}

MethodBeforeAdviceBarImpl.java

1
2
3
4
5
6
7
8
9
10
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!");
    }
}

最后是App.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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();
    }
}

在App.java中运行main方法将输出:

1
2
3
Bar!
Foo!
Baz!

因此,正如您在此处看到的,NameMatchMethodPointcutAdvisor是一个Advisor,它由一个mapedName(这是切入点)和Advice本身(在本例中为MethodBeforeAdvice)组成。

在面向方面的编程术语中,方面就是"建议+切入点",所以您就可以了。顾问似乎毕竟是方面。


当Java 5的使用仍然不太常见,因此Spring不再使用@AspectJ语法(通过Java注释)时,顾问似乎是从Spring 1.2定义横切关注点的一种古老的" AOP lite"类型。对于不喜欢基于注释的AOP或纯粹的AspectJ语法的基于模式的AOP的爱好者来说,这一概念仍然存在,请参阅关于顾问的Spring文档。

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.


Advice是对Pointcut采取操作的方式。您可以在建议之前,之后甚至周围使用建议来应用您定义的任何操作。谈到Spring Aspect,它只是一个高级类,它合并了两个概念:joinpointAdvice。可以通过基于XML的蓝图或以编程方式完成。另外,您应该指定要插入方面的点,这是使用joinpoint完成的。

此外,Spring AspectsAdvice不能互相替代,因为Aspects只是合并联接和建议的对象。