此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
作 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()
method 返回一个Advisor
对于每个 advisor、interceptor 或
其他已添加到 Factory 的通知类型。如果您添加了Advisor
这
在此索引返回的 advisor 是您添加的对象。如果您添加了
interceptor 或其他 advice 类型,Spring 将其包装在 advisor 中,并带有
始终返回的切入点true
.因此,如果您添加了MethodInterceptor
、顾问
为此索引返回的是一个DefaultPointcutAdvisor
这会返回您的MethodInterceptor
以及匹配所有类和方法的切入点。
这addAdvisor()
methods 可用于添加任何Advisor
.通常,顾问持有
切入点和建议是通用的DefaultPointcutAdvisor
,您可以将其与
任何建议或切入点(但不包括介绍)。
默认情况下,可以添加或删除 advisor 或拦截器,即使一个 proxy 已创建。唯一的限制是无法添加或删除 Introduction Advisor,因为工厂的现有代理不显示界面 改变。(您可以从工厂获取新的 proxy 以避免此问题。
以下示例显示了将 AOP 代理转换为Advised
接口和 examine 以及
纵其建议:
-
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)
修改 business 对象,尽管毫无疑问存在合法的用例。 但是,它在开发中可能非常有用(例如,在测试中)。我们有时 发现能够以拦截器或其他 建议,进入我们想要测试的方法调用。(例如,建议可以 进入为该方法创建的事务中,也许是为了运行 SQL 来检查它 在将事务标记为回滚之前,已正确更新了数据库。 |
根据您创建代理的方式,您通常可以将frozen
旗。在那个
case 中,使用Advised
isFrozen()
method 返回true
以及任何修改
建议AopConfigException
.能力
冻结被通知对象的 state 在某些情况下很有用(例如,冻结
防止调用代码删除安全拦截器)。