【源码】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
  • 它维护整个代理行为对应的 Advisors Advice

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

总结

Advised 接口主要是抽象整个代理的行为,比如维护具体的 Advisors Advices,提供具体的 Interceptors Chains,提供具体的创建代理方法

它的三个实现类可以直接用来创建代理对象,下一章节具体了解这三个实现类

上一篇:【源码】Spring AOP 8 TargetSource AdvisorAdapter AdvisorAdapterRegistry AdvisorChainFactory
下一篇:【源码】Spring AOP 10 ProxyFactoryBean ProxyFactory AspectJProxyFactory

参考

【小家Spring】Spring AOP各个组件概述与总结【Pointcut、Advice、Advisor、Advised、TargetSource、AdvisorChainFactory…】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值