对于最新稳定版本,请使用Spring Framework 7.0.1spring-doc.cadn.net.cn

作建议对象

无论你如何创建AOP代理,你都可以通过使用org.springframework.aop.framework.建议接口。任何AOP代理都可以投射到这里 无论它实现了哪些其他接口。该接口包括 方法如下:spring-doc.cadn.net.cn

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()方法返回顾问对于每个顾问、拦截机,或 工厂中新增的其他建议类型。如果你添加了一个顾问这 在这个索引中返回的顾问就是你添加的对象。如果你添加了一个 拦截机或其他建议类型,Spring用一个顾问包裹了这个 总是返回的点切割true.因此,如果你添加了一个拦截方法, 顾问 该索引返回的是一个默认点cutAdvisor这会返回你的拦截方法以及一个匹配所有类和方法的点割。spring-doc.cadn.net.cn

addAdvisor()可以用来添加任意方法顾问.通常,顾问持有 Pointcut 和 Advice 是通用的默认点cutAdvisor,你可以将其与 有什么建议或建议吗(但不是介绍)。spring-doc.cadn.net.cn

默认情况下,即使代理存在,也可以添加或移除顾问或拦截者 已经被创造出来。唯一的限制是无法添加或删除 介绍顾问,因为工厂现有的代理文件不显示该接口 改变。(你可以从工厂获得新的代理以避免这个问题。)spring-doc.cadn.net.cn

以下示例展示了将AOP代理投射到以下建议接口与检查 控其建议:spring-doc.cadn.net.cn

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来检查 数据库在标记回滚交易前已正确更新。)

根据你如何创建代理,通常可以设置冷冻旗。在那个地方 情况,建议 isFrozen()方法返回true,以及任何修改尝试 通过添加或删除获得的建议结果是AopConfigException.该能力 冻结被建议对象的状态在某些情况下是有用的(例如,对 防止调用代码,移除安全拦截器)。spring-doc.cadn.net.cn