操作增强(Advised)对象

无论如何创建 AOP 代理,您都可以通过使用 org.springframework.aop.framework.Advised 接口来操作它们。任何 AOP 代理都可以被强制转换为此接口,无论它实现了任何其他接口。此接口包含以下方法

  • Java

  • Kotlin

Advisor[] getAdvisors();

void addAdvice(Advice advice) throws AopConfigException;

void addAdvice(int pos, Advice advice) throws AopConfigException;

void addAdvisor(Advisor advisor) throws AopConfigException;

void addAdvisor(int pos, Advisor advisor) throws AopConfigException;

int indexOf(Advisor advisor);

boolean removeAdvisor(Advisor advisor) throws AopConfigException;

void removeAdvisor(int index) throws AopConfigException;

boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException;

boolean isFrozen();
fun getAdvisors(): Array<Advisor>

@Throws(AopConfigException::class)
fun addAdvice(advice: Advice)

@Throws(AopConfigException::class)
fun addAdvice(pos: Int, advice: Advice)

@Throws(AopConfigException::class)
fun addAdvisor(advisor: Advisor)

@Throws(AopConfigException::class)
fun addAdvisor(pos: Int, advisor: Advisor)

fun indexOf(advisor: Advisor): Int

@Throws(AopConfigException::class)
fun removeAdvisor(advisor: Advisor): Boolean

@Throws(AopConfigException::class)
fun removeAdvisor(index: Int)

@Throws(AopConfigException::class)
fun replaceAdvisor(a: Advisor, b: Advisor): Boolean

fun isFrozen(): Boolean

getAdvisors() 方法为添加到工厂中的每个 advisor、interceptor 或其他通知类型返回一个 Advisor。如果您添加了一个 Advisor,则此索引处返回的 advisor 就是您添加的对象。如果您添加了 interceptor 或其他通知类型,Spring 会将其封装在一个 pointcut 始终返回 true 的 advisor 中。因此,如果您添加了一个 MethodInterceptor,则此索引处返回的 advisor 是一个 DefaultPointcutAdvisor,它返回您的 MethodInterceptor 和匹配所有类和方法的 pointcut。

addAdvisor() 方法可用于添加任何 Advisor。通常,持有 pointcut 和 advice 的 advisor 是通用的 DefaultPointcutAdvisor,您可以将其用于任何 advice 或 pointcut(但不适用于引介)。

默认情况下,即使代理已创建,也可以添加或移除 advisor 或 interceptor。唯一的限制是无法添加或移除引介 advisor,因为工厂中现有的代理不会显示接口更改。(您可以从工厂获取一个新代理来避免此问题。)

以下示例演示了将 AOP 代理强制转换为 Advised 接口以及检查和操作其通知:

  • Java

  • Kotlin

Advised advised = (Advised) myObject;
Advisor[] advisors = advised.getAdvisors();
int oldAdvisorCount = advisors.length;
System.out.println(oldAdvisorCount + " advisors");

// Add an advice like an interceptor without a pointcut
// Will match all proxied methods
// Can use for interceptors, before, after returning or throws advice
advised.addAdvice(new DebugInterceptor());

// Add selective advice using a pointcut
advised.addAdvisor(new DefaultPointcutAdvisor(mySpecialPointcut, myAdvice));

assertEquals("Added two advisors", oldAdvisorCount + 2, advised.getAdvisors().length);
val advised = myObject as Advised
val advisors = advised.advisors
val oldAdvisorCount = advisors.size
println("$oldAdvisorCount advisors")

// Add an advice like an interceptor without a pointcut
// Will match all proxied methods
// Can use for interceptors, before, after returning or throws advice
advised.addAdvice(DebugInterceptor())

// Add selective advice using a pointcut
advised.addAdvisor(DefaultPointcutAdvisor(mySpecialPointcut, myAdvice))

assertEquals("Added two advisors", oldAdvisorCount + 2, advised.advisors.size)
在生产环境中修改业务对象的通知是否明智,这一点值得商榷,尽管无疑存在合法的用例。但是,这在开发中(例如,在测试中)可能非常有用。我们有时发现能够以 interceptor 或其他通知的形式添加测试代码非常有用,从而进入我们想要测试的方法调用内部。(例如,通知可以进入为该方法创建的事务内部,也许在标记事务回滚之前运行 SQL 以检查数据库是否已正确更新。)

根据创建代理的方式,您通常可以设置一个 frozen 标志。在这种情况下,Advised 接口的 isFrozen() 方法返回 true,任何通过添加或移除来修改通知的尝试都会导致 AopConfigException。在某些情况下,冻结增强(advised)对象的状态很有用(例如,防止调用代码移除安全 interceptor)。