【源码】Spring AOP 9 Advised
前言
Advised ,个人理解为对整个代理执行的抽象
Advised
// TargetClassAware 目标类的维护
public interface Advised extends TargetClassAware {
// 配置冻结,则 advice 不可改变
boolean isFrozen();
// 是否代理目标类(而不是接口),关系到代理方式(JDK | CGLIB)
boolean isProxyTargetClass();
// 返回 target class 以外的所有接口
Class<?>[] getProxiedInterfaces();
// 确定给定接口是否被代理
boolean isInterfaceProxied(Class<?> intf);
// 以 Advised 对象改变 TargetSource(注意isFrozen)
void setTargetSource(TargetSource targetSource);
TargetSource getTargetSource();
/**
* 是否允许 AOP 框架以 ThreadLocal 的形式将代理对象暴露出去,
* 允许通过 AopContext 访问。这个属性默认为 false。
* 设置为 true 时,允许我们使用 AopConext.currentProxy()
* 获取当前代理对象(内部以 NamedThreadLocal 维护)
*/
void setExposeProxy(boolean exposeProxy);
boolean isExposeProxy();
/**
* 此属性默认为false,设置为 true 时,
* 创建对象时获取 advisor chain 可以跳过 ClassFilter 检查
*/
void setPreFiltered(boolean preFiltered);
boolean isPreFiltered();
// Advisor 相关操作
Advisor[] getAdvisors();
void addAdvisor(Advisor advisor) throws AopConfigException;
void addAdvisor(int pos, Advisor advisor) throws AopConfigException;
boolean removeAdvisor(Advisor advisor);
void removeAdvisor(int index) throws AopConfigException;
int indexOf(Advisor advisor);
boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException;
// advice 相关操作
void addAdvice(Advice advice) throws AopConfigException;
void addAdvice(int pos, Advice advice) throws AopConfigException;
boolean removeAdvice(Advice advice);
int indexOf(Advice advice);
String toProxyConfigString();
}
- 它维护代理相关的一些属性
frozen
proxyTargetClass
等 - 它维护整个代理行为对应的
Advisor
sAdvice
等
AdvisedSupport
public class AdvisedSupport extends ProxyConfig implements Advised {
// 基于 List 管理的 Advisor 的相关操作...
// Advice 通常都是封装成 DefaultPointcutAdvisor 进行操作...
AdvisorChainFactory advisorChainFactory = new DefaultAdvisorChainFactory();
// 基于 DefaultAdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice 方法实现
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, @Nullable Class<?> targetClass) {
MethodCacheKey cacheKey = new MethodCacheKey(method);
List<Object> cached = this.methodCache.get(cacheKey);
if (cached == null) {
cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
this, method, targetClass);
this.methodCache.put(cacheKey, cached);
}
return cached;
}
}
- 作为一个基类实现了提供了
Advisor
Advice
管理的相关方法实现 - 额外提供了方法
getInterceptorsAndDynamicInterceptionAdvice
是基于成员属性DefaultAdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice
来实现的,这个之前了解过
ProxyCreatorSupport
public class ProxyCreatorSupport extends AdvisedSupport {
// ...
public ProxyCreatorSupport() {
this.aopProxyFactory = new DefaultAopProxyFactory();
}
public ProxyCreatorSupport(AopProxyFactory aopProxyFactory) {
this.aopProxyFactory = aopProxyFactory;
}
// 创建代理,使用代理创建器工厂创建代理创建器,用代理创建器创建代理
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
return getAopProxyFactory().createAopProxy(this);
}
}
- 在
AdvisedSupport
的基础上提供了代理创建方法createAopProxy
createAopProxy
方法委托AopProxyFactory
实现,构造方法允许指定AopProxyFactory
,默认DefaultAopProxyFactory
,之后会了解
类图
总结
Advised
接口主要是抽象整个代理的行为,比如维护具体的 Advisor
s Advice
s,提供具体的 Interceptors Chains
,提供具体的创建代理方法
它的三个实现类可以直接用来创建代理对象,下一章节具体了解这三个实现类
上一篇:【源码】Spring AOP 8 TargetSource AdvisorAdapter AdvisorAdapterRegistry AdvisorChainFactory
下一篇:【源码】Spring AOP 10 ProxyFactoryBean ProxyFactory AspectJProxyFactory
参考
【小家Spring】Spring AOP各个组件概述与总结【Pointcut、Advice、Advisor、Advised、TargetSource、AdvisorChainFactory…】