基于Spring源码分析AOP的实现机制
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public Object
getObject() throws BeansException
{ //初始化通知器链,实际上就是注册拦截器 initializeAdvisorChain(); if (isSingleton())
{ //返回生成的一个单件Proxy return getSingletonInstance(); } else { .... //返回生成的Prototype的Proxy return newPrototypeInstance(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
private synchronized Object
getSingletonInstance() { if ( this .singletonInstance
== null ) { this .targetSource
= freshTargetSource(); if ( this .autodetectInterfaces
&& getProxiedInterfaces().length == 0 &&
!isProxyTargetClass()) { //获取要代理的类 Class
targetClass = getTargetClass(); ...设置该类的接口类型 setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this .proxyClassLoader)); } super .setFrozen( this .freezeProxy); //这里才是真正的获取Proxy, this .singletonInstance
= getProxy(createAopProxy()); } return this .singletonInstance; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public AopProxy
createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize()
|| config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class
targetClass = config.getTargetClass(); //代理类为空的时候 if (targetClass
== null )
{ .... } //
代理对象为接口的时候 if (targetClass.isInterface())
{ return new JdkDynamicAopProxy(config); } if (!cglibAvailable)
{ } eturn
CglibProxyFactory.createCglibProxy(config); } else { return new JdkDynamicAopProxy(config); } |
1
2
3
4
5
6
7
|
public Object
getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled())
{ ....} Class[]
proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces( this .advised); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); return Proxy.newProxyInstance(classLoader,
proxiedInterfaces, this ); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
public Object
invoke(Object proxy, Method method, Object[] args) throws Throwable
{ MethodInvocation
invocation; Object
oldProxy = null ; boolean setProxyContext
= false ; TargetSource
targetSource = this .advised.targetSource; Class
targetClass = null ; Object
target = null ; try { if (! this .equalsDefined
&& AopUtils.isEqualsMethod(method)) { return equals(args[ 0 ]); } if (! this .hashCodeDefined
&& AopUtils.isHashCodeMethod(method)) { return hashCode(); } if (! this .advised.opaque
&& method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised. class ))
{ //
利用Proxy配置来调用服务,直接调用目标方法 return AopUtils.invokeJoinpointUsingReflection( this .advised,
method, args); } Object
retVal; if ( this .advised.exposeProxy)
{ oldProxy
= AopContext.setCurrentProxy(proxy); setProxyContext
= true ; } //获取所要代理的对象 target
= targetSource.getTarget(); if (target
!= null )
{ targetClass
= target.getClass(); } //
获得该方法上的拦截器链 List<Object>
chain = this .advised.getInterceptorsAndDynamicInterceptionAdvice(method,
targetClass); //是否定义拦截器,否则直接调用目标对象的方法 if (chain.isEmpty())
{ //直接调用目标对象的方法 retVal
= AopUtils.invokeJoinpointUsingReflection(target, method, args); } else { //如果不为空,则就创建一个ReflectiveMethodInvocation对象来先调用拦截器后调用目标方法 invocation
= new ReflectiveMethodInvocation (proxy,
target, method, args, targetClass, chain); //
处理切入点上的拦截器的方法 retVal
= invocation.proceed(); } if (retVal
!= null &&
retVal == target && method.getReturnType().isInstance(proxy) && !RawTargetAccess. class .isAssignableFrom(method.getDeclaringClass()))
{ retVal
= proxy; } return retVal; } finally { if (target
!= null &&
!targetSource.isStatic()) { targetSource.releaseTarget(target); } if (setProxyContext)
{ AopContext.setCurrentProxy(oldProxy); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public Object
proceed() throws Throwable
{ //直接调用目标方法,可能是拦截器调用结束或者无拦截器 //interceptorsAndDynamicMethodMatchers这个其实就是目标方法上的拦截器链的大小 if ( this .currentInterceptorIndex
== this .interceptorsAndDynamicMethodMatchers.size()
- 1 )
{ return invokeJoinpoint(); } //调用拦截器链上的对象,依次 Object
interceptorOrInterceptionAdvice = this .interceptorsAndDynamicMethodMatchers.get(++ this .currentInterceptorIndex); if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) { //
这里获得相应的拦截器,如果拦截器可以匹配的上的话,那就调用拦截器的invoke 方法 InterceptorAndDynamicMethodMatcher
dm = (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; if (dm.methodMatcher.matches( this .method, this .targetClass, this .arguments))
{ return dm.interceptor.invoke( this ); } else { //调用拦截器链中的下一个拦截器 return proceed(); } } else { //
It's an interceptor, so we just invoke it: The pointcut will have //
been evaluated statically before this object was constructed. return ((MethodInterceptor)
interceptorOrInterceptionAdvice).invoke( this ); } } |
如果你喜欢本文, 请长按二维码,关注公众号 分布式编程.
作者:分布式编程
出处:https://zthinker.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。