基于Spring源码分析AOP的实现机制
来源:互联网 发布:科禄格风机选型软件 编辑:程序博客网 时间:2024/06/10 20:14
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
);
}
}
0 0
- 基于Spring源码分析AOP的实现机制
- Spring AOP的实现机制(三)---@AspectJ形式的Spring AOP和基于Schema的AOP
- Spring源码分析:实现AOP- -
- Spring实现AOP源码分析
- Spring AOP的实现机制
- SPRING AOP的实现机制
- spring aop的实现机制
- Spring AOP的实现机制
- Spring AOP源码分析(拦截器调用的实现)
- Spring AOP 源码分析 part4 :拦截器的实现
- Spring Aop实现机制分析原理
- spring aop源码分析
- Spring Aop源码分析
- spring aop 源码分析
- Spring源码分析----AOP概念(Advice,Pointcut,Advisor)和AOP的设计与实现
- Spring源码分析----AOP概念(Advice,Pointcut,Advisor)和AOP的设计与实现
- Dubbo源码分析 ---- 基于SPI的扩展实现机制
- 探析Spring AOP(二):Spring AOP的实现机制
- CUDA开发环境配置
- Emacs+Python(非默认模式)快速配置
- 【题解】[hdu3874]Necklace
- ubuntu /proc/bus/usb 没有的解决方法
- java 中的printStackTrace()方法
- 基于Spring源码分析AOP的实现机制
- 高斯平滑滤波器
- hdu1849Rabbit and Grass (尼姆博弈)
- typedef 与 #define的区别
- 卡特兰数 Catalan number
- 读usbfs的一些感想
- 编辑文章 - 博客频道 - CSDN.NET
- 无法加载web信息(端口占用问题)
- C++单一类的定义与使用