1、springAOP之ProxyFactoryspringAOP之ProxyFactoryProxyFactory就springAOP创建代理的工厂,现在以此为入口进行aop源码的解读。首先看ProxyFactory的结构图:TargetClassAware根据结构图,先看接口TargetClassAware,该接口是暴露代理的目标类的最小实现,该接口只有一个方法,Class getTargetClass();1获取实现对象的目标类,当目标类未知,返回null。接口Advised接下来看它的子接口Advised, 接口说明: 由类持有一个AOP代理工厂配置的实现,配置包拦截器、其他的通知、顾问、
2、代理的接口。 spring的任何AOP代理都能强转为此接口,这样就能够允许操作aop的通知。 接口方法如下:/通知配置是否冻结,冻结后通知不能做改变boolean isFrozen();/是否代理目标类boolean isProxyTargetClass();/返回aop的代理接口,不包括目标类Class getProxiedInterfaces();boolean isInterfaceProxied(Class intf);void setTargetSource(TargetSource targetSource);TargetSource getTargetSource();/通过ao
3、p框架,作为用AopContext可检索的ThreadLocal向外暴露 /当一个advised object需要调用另外一个advised object时有用。void setExposeProxy(boolean exposeProxy);boolean isExposeProxy();/设置代理配置为提前过滤,因此它包括适当的advisors(通过匹配代理目标类)void setPreFiltered(boolean preFiltered);boolean isPreFiltered();Advisor getAdvisors();void addAdvisor(Advisor adv
4、isor) 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;void
5、addAdvice(Advice advice) throws AopConfigException;void addAdvice(int pos, Advice advice) throws AopConfigException;boolean removeAdvice(Advice advice);int indexOf(Advice advice);String toProxyConfigString();ProxyConfig类:方便的超类对于创建代理的配置,确保所以的代理创建对象有一致的属性。 方法如下: public void setProxyTargetClass(boolean
6、 proxyTargetClass) this.proxyTargetClass = proxyTargetClass; public boolean isProxyTargetClass() return this.proxyTargetClass; public void setOptimize(boolean optimize) this.optimize = optimize; public boolean isOptimize() return this.optimize; public void setOpaque(boolean opaque) this.opaque = opa
7、que; public boolean isOpaque() return this.opaque; public void setExposeProxy(boolean exposeProxy) this.exposeProxy = exposeProxy; public boolean isExposeProxy() return this.exposeProxy; public void setFrozen(boolean frozen) this.frozen = frozen; public boolean isFrozen() return this.frozen; public
8、void copyFrom(ProxyConfig other) Assert.notNull(other, Other ProxyConfig object must not be null); this.proxyTargetClass = other.proxyTargetClass; this.optimize = other.optimize; this.exposeProxy = other.exposeProxy; this.frozen = other.frozen; this.opaque = other.opaque; Override public String toSt
9、ring() StringBuilder sb = new StringBuilder(); sb.append(proxyTargetClass=).append(this.proxyTargetClass).append(; ); sb.append(optimize=).append(this.optimize).append(; ); sb.append(opaque=).append(this.opaque).append(; ); sb.append(exposeProxy=).append(this.exposeProxy).append(; ); sb.append(froze
10、n=).append(this.frozen); return sb.toString(); 发现其中有一些方法是为了实现Advised接口而准备的。AdvisedSupport类:aop代理配置管理的基类。 自身不是aop代理,但其子类是能够直接获取aop代理实例的常规工厂。 该类为子类管理创建Advices和Advisors利用便利,但它实际上没有实现代理的创建方法,由子类各自提供。构造函数代码实现: private transient MapMethodCacheKey, List methodCache; / public AdvisedSupport() initMethodCach
11、e(); public AdvisedSupport(Class interfaces) this(); setInterfaces(interfaces); /默认初始化是创建32个map private void initMethodCache() this.methodCache = new ConcurrentHashMapMethodCacheKey, List(32); 对于带接口数组参数的构造函数,设置先清空,后重新设置: private ListClass interfaces = new ArrayListClass(); public void setInterfaces(
12、Class. interfaces) Assert.notNull(interfaces, Interfaces must not be null); this.interfaces.clear(); for (Class ifc : interfaces) addInterface(ifc); public void addInterface(Class intf) Assert.notNull(intf, Interface must not be null); if (!intf.isInterface() throw new IllegalArgumentException( + in
13、tf.getName() + is not an interface); if (!this.interfaces.contains(intf) this.interfaces.add(intf); /当代理接口有更改,也同时清空methodCache adviceChanged(); protected void adviceChanged() this.methodCache.clear(); public boolean removeInterface(Class intf) return this.interfaces.remove(intf); Override public Cla
14、ss getProxiedInterfaces() return this.interfaces.toArray(new Classthis.interfaces.size(); /判断所有的目标类是否是接口 Override public boolean isInterfaceProxied(Class intf) for (Class proxyIntf : this.interfaces) if (intf.isAssignableFrom(proxyIntf) return true; return false; 设置代理目标的方法: public static final Targe
15、tSource EMPTY_TARGET_SOURCE = EmptyTargetSource.INSTANCE; TargetSource targetSource = EMPTY_TARGET_SOURCE; /将根据给得的object创建 SingletonTargetSource public void setTarget(Object target) setTargetSource(new SingletonTargetSource(target); /直接设置TargetSource相应的接口实现,若为空,返回EMPTY_TARGET_SOURCE Override public
16、void setTargetSource(TargetSource targetSource) this.targetSource = (targetSource != null ? targetSource : EMPTY_TARGET_SOURCE); /该代理目标类能够转为targetClass,该方法是前面两个方法的替代:当我们想要一个没有可达TargetSource的代理类 public void setTargetClass(Class targetClass) this.targetSource = EmptyTargetSource.forClass(targetClass);
17、 Override public Class getTargetClass() return this.targetSource.getTargetClass(); 设置是否预先过滤,默认为false: private boolean preFiltered = false; Override public void setPreFiltered(boolean preFiltered) this.preFiltered = preFiltered; Override public boolean isPreFiltered() return this.preFiltered; 设置Advis
18、orChainFactory: AdvisorChainFactory advisorChainFactory = new DefaultAdvisorChainFactory(); public void setAdvisorChainFactory(AdvisorChainFactory advisorChainFactory) Assert.notNull(advisorChainFactory, AdvisorChainFactory must not be null); this.advisorChainFactory = advisorChainFactory; public Ad
19、visorChainFactory getAdvisorChainFactory() return this.advisorChainFactory; 对于Advisor的操作: private Advisor advisorArray = new Advisor0; /获取操作 protected final List getAdvisorsInternal() return this.advisors; Override public final Advisor getAdvisors() return this.advisorArray; /添加操作 Override public vo
20、id addAdvisor(Advisor advisor) int pos = this.advisors.size(); addAdvisor(pos, advisor); Override public void addAdvisor(int pos, Advisor advisor) throws AopConfigException if (advisor instanceof IntroductionAdvisor) validateIntroductionAdvisor(IntroductionAdvisor) advisor); addAdvisorInternal(pos,
21、advisor); private void validateIntroductionAdvisor(IntroductionAdvisor advisor) advisor.validateInterfaces(); / If the advisor passed validation, we can make the change. Class ifcs = advisor.getInterfaces(); for (Class ifc : ifcs) addInterface(ifc); private void addAdvisorInternal(int pos, Advisor a
22、dvisor) throws AopConfigException Assert.notNull(advisor, Advisor must not be null); if (isFrozen() throw new AopConfigException(Cannot add advisor: Configuration is frozen.); if (pos this.advisors.size() throw new IllegalArgumentException( Illegal position + pos + in advisor list with size + this.a
23、dvisors.size(); this.advisors.add(pos, advisor); updateAdvisorArray(); adviceChanged(); protected final void updateAdvisorArray() this.advisorArray = this.advisors.toArray(new Advisorthis.advisors.size(); /对于多个Advisor的添加 public void addAdvisors(Advisor. advisors) addAdvisors(Arrays.asList(advisors);
24、 /对于Advisor集合的添加 public void addAdvisors(Collection advisors) if (isFrozen() throw new AopConfigException(Cannot add advisor: Configuration is frozen.); if (!CollectionUtils.isEmpty(advisors) for (Advisor advisor : advisors) if (advisor instanceof IntroductionAdvisor) validateIntroductionAdvisor(Int
25、roductionAdvisor) advisor); Assert.notNull(advisor, Advisor must not be null); this.advisors.add(advisor); updateAdvisorArray(); adviceChanged(); /删除操作 Override public boolean removeAdvisor(Advisor advisor) int index = indexOf(advisor); if (index = -1) return false; else removeAdvisor(index); return
26、 true; Override public int indexOf(Advisor advisor) Assert.notNull(advisor, Advisor must not be null); return this.advisors.indexOf(advisor); Override public void removeAdvisor(int index) throws AopConfigException if (isFrozen() throw new AopConfigException(Cannot remove Advisor: Configuration is fr
27、ozen.); if (index this.advisors.size() - 1) throw new AopConfigException(Advisor index + index + is out of bounds: + This configuration only has + this.advisors.size() + advisors.); Advisor advisor = this.advisors.get(index); if (advisor instanceof IntroductionAdvisor) IntroductionAdvisor ia = (Intr
28、oductionAdvisor) advisor; / We need to remove introduction interfaces. for (int j = 0; j ia.getInterfaces().length; j+) removeInterface(ia.getInterfaces()j); this.advisors.remove(index); updateAdvisorArray(); adviceChanged(); /替换操作 Override public boolean replaceAdvisor(Advisor a, Advisor b) throws
29、AopConfigException Assert.notNull(a, Advisor a must not be null); Assert.notNull(b, Advisor b must not be null); int index = indexOf(a); if (index = -1) return false; removeAdvisor(index); addAdvisor(index, b); return true; 对于Advice的操作: Override public void addAdvice(Advice advice) throws AopConfigException int pos = this.advisors.size(); addAdvice(pos, advice); Override public void addAdvice(int pos, Advice advice) throws AopConfigException Asse
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1