操作被建议的对象

无论您如何创建 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。如果您添加了一个Advisor,则此索引处返回的顾问是您添加的对象。如果您添加了拦截器或其他建议类型,Spring 会将其包装在一个顾问中,该顾问具有始终返回true 的切点。因此,如果您添加了一个MethodInterceptor,则为此索引返回的顾问是一个DefaultPointcutAdvisor,它返回您的MethodInterceptor 和一个匹配所有类和方法的切点。

addAdvisor() 方法可用于添加任何Advisor。通常,包含切点和建议的顾问是泛型DefaultPointcutAdvisor,您可以将其与任何建议或切点一起使用(但不能用于引入)。

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

以下示例显示了将 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)
在生产环境中修改业务对象的建议是否可取(并非有意为之)是有疑问的,尽管毫无疑问存在合理的用例。但是,它在开发中(例如,在测试中)非常有用。我们有时发现能够以拦截器或其他建议的形式添加测试代码非常有用,从而进入我们想要测试的方法调用。(例如,建议可以进入为该方法创建的事务中,也许是运行 SQL 以检查数据库是否已正确更新,然后将事务标记为回滚。)

根据您创建代理的方式,您通常可以设置一个frozen 标志。在这种情况下,AdvisedisFrozen() 方法返回true,并且任何尝试通过添加或删除来修改建议都会导致AopConfigException。能够冻结已建议对象的状体在某些情况下非常有用(例如,为了防止调用代码删除安全拦截器)。