1.简介

本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程。现在我们的得到了 bean 的代理对象,且通知也以合适的方式插在了目标方法的前后。接下来要做的事情,就是执行通知逻辑了。通知可能在目标方法前执行,也可能在目标方法后执行。具体的执行时机,取决于用户的配置。当目标方法被多个通知匹配到时,Spring 通过引入拦截器链来保证每个通知的正常执行。在本文中,我们将会通过源码了解到 Spring 是如何支持 expose-proxy 属性的,以及通知与拦截器之间的关系,拦截器链的执行过程等。和上一篇文章一样,在进行源码分析前,我们先来了解一些背景知识。好了,下面进入正题吧。

2.背景知识

关于 expose-proxy,我们先来说说它有什么用,然后再来说说怎么用。Spring 引入 expose-proxy 特性是为了解决目标方法调用同对象中其他方法时,其他方法的切面逻辑无法执行的问题。这个解释可能不好理解,不直观。那下面我来演示一下它的用法,大家就知道是怎么回事了。我们先来看看 expose-proxy 是怎样配置的,如下:

  1. <bean id="hello" class="xyz.coolblog.aop.Hello"/>
  2. <bean id="aopCode" class="xyz.coolblog.aop.AopCode"/>
  3. <aop:aspectj-autoproxy expose-proxy="true" />
  4. <aop:config expose-proxy="true">
  5. <aop:aspect id="myaspect" ref="aopCode">
  6. <aop:pointcut id="helloPointcut" expression="execution(* xyz.coolblog.aop.*.hello*(..))" />
  7. <aop:before method="before" pointcut-ref="helloPointcut" />
  8. </aop:aspect>
  9. </aop:config>

如上,expose-proxy 可配置在 <aop:config/><aop:aspectj-autoproxy /> 标签上。在使用 expose-proxy 时,需要对内部调用进行改造,比如:

  1. public class Hello implements IHello {
  2. @Override
  3. public void hello() {
  4. System.out.println("hello");
  5. this.hello("world");
  6. }
  7. @Override
  8. public void hello(String hello) {
  9. System.out.println("hello " + hello);
  10. }
  11. }

hello()方法调用了同类中的另一个方法hello(String),此时hello(String)上的切面逻辑就无法执行了。这里,我们要对hello()方法进行改造,强制它调用代理对象中的hello(String)。改造结果如下:

  1. public class Hello implements IHello {
  2. @Override
  3. public void hello() {
  4. System.out.println("hello");
  5. ((IHello) AopContext.currentProxy()).hello("world");
  6. }
  7. @Override
  8. public void hello(String hello) {
  9. System.out.println("hello " + hello);
  10. }
  11. }

如上,AopContext.currentProxy()用于获取当前的代理对象。当 expose-proxy 被配置为 true 时,该代理对象会被放入 ThreadLocal 中。关于 expose-proxy,这里先说这么多,后面分析源码时会再次提及。

3.源码分析

本章所分析的源码来自 JdkDynamicAopProxy,至于 CglibAopProxy 中的源码,大家若有兴趣可以自己去看一下。

3.1 JDK 动态代理逻辑分析

本节,我来分析一下 JDK 动态代理逻辑。对于 JDK 动态代理,代理逻辑封装在 InvocationHandler 接口实现类的 invoke 方法中。JdkDynamicAopProxy 实现了 InvocationHandler 接口,下面我们就来分析一下 JdkDynamicAopProxy 的 invoke 方法。如下:

  1. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  2. MethodInvocation invocation;
  3. Object oldProxy = null;
  4. boolean setProxyContext = false;
  5. TargetSource targetSource = this.advised.targetSource;
  6. Class<?> targetClass = null;
  7. Object target = null;
  8. try {
  9. // 省略部分代码
  10. Object retVal;
  11. // 如果 expose-proxy 属性为 true,则暴露代理对象
  12. if (this.advised.exposeProxy) {
  13. // 向 AopContext 中设置代理对象
  14. oldProxy = AopContext.setCurrentProxy(proxy);
  15. setProxyContext = true;
  16. }
  17. // 获取适合当前方法的拦截器
  18. List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
  19. // 如果拦截器链为空,则直接执行目标方法
  20. if (chain.isEmpty()) {
  21. Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
  22. // 通过反射执行目标方法
  23. retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
  24. }
  25. else {
  26. // 创建一个方法调用器,并将拦截器链传入其中
  27. invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
  28. // 执行拦截器链
  29. retVal = invocation.proceed();
  30. }
  31. // 获取方法返回值类型
  32. Class<?> returnType = method.getReturnType();
  33. if (retVal != null && retVal == target &&
  34. returnType != Object.class && returnType.isInstance(proxy) &&
  35. !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
  36. // 如果方法返回值为 this,即 return this; 则将代理对象 proxy 赋值给 retVal
  37. retVal = proxy;
  38. }
  39. // 如果返回值类型为基础类型,比如 int,long 等,当返回值为 null,抛出异常
  40. else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
  41. throw new AopInvocationException(
  42. "Null return value from advice does not match primitive return type for: " + method);
  43. }
  44. return retVal;
  45. }
  46. finally {
  47. if (target != null && !targetSource.isStatic()) {
  48. targetSource.releaseTarget(target);
  49. }
  50. if (setProxyContext) {
  51. AopContext.setCurrentProxy(oldProxy);
  52. }
  53. }
  54. }

如上,上面的代码我做了比较详细的注释。下面我们来总结一下 invoke 方法的执行流程,如下:

  1. 检测 expose-proxy 是否为 true,若为 true,则暴露代理对象
  2. 获取适合当前方法的拦截器
  3. 如果拦截器链为空,则直接通过反射执行目标方法
  4. 若拦截器链不为空,则创建方法调用 ReflectiveMethodInvocation 对象
  5. 调用 ReflectiveMethodInvocation 对象的 proceed() 方法启动拦截器链
  6. 处理返回值,并返回该值

在以上6步中,我们重点关注第2步和第5步中的逻辑。第2步用于获取拦截器链,第5步则是启动拦截器链。下面先来分析获取拦截器链的过程。

3.2 获取所有的拦截器

所谓的拦截器,顾名思义,是指用于对目标方法的调用进行拦截的一种工具。拦截器的源码比较简单,所以我们直接看源码好了。下面以前置通知拦截器为例,如下:

  1. public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {
  2. /** 前置通知 */
  3. private MethodBeforeAdvice advice;
  4. public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
  5. Assert.notNull(advice, "Advice must not be null");
  6. this.advice = advice;
  7. }
  8. @Override
  9. public Object invoke(MethodInvocation mi) throws Throwable {
  10. // 执行前置通知逻辑
  11. this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
  12. // 通过 MethodInvocation 调用下一个拦截器,若所有拦截器均执行完,则调用目标方法
  13. return mi.proceed();
  14. }
  15. }

如上,前置通知的逻辑在目标方法执行前被执行。这里先简单向大家介绍一下拦截器是什么,关于拦截器更多的描述将放在下一节中。本节我们先来看看如何如何获取拦截器,如下:

  1. public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, Class<?> targetClass) {
  2. MethodCacheKey cacheKey = new MethodCacheKey(method);
  3. // 从缓存中获取
  4. List<Object> cached = this.methodCache.get(cacheKey);
  5. // 缓存未命中,则进行下一步处理
  6. if (cached == null) {
  7. // 获取所有的拦截器
  8. cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
  9. this, method, targetClass);
  10. // 存入缓存
  11. this.methodCache.put(cacheKey, cached);
  12. }
  13. return cached;
  14. }
  15. public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
  16. Advised config, Method method, Class<?> targetClass) {
  17. List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length);
  18. Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
  19. boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
  20. // registry 为 DefaultAdvisorAdapterRegistry 类型
  21. AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
  22. // 遍历通知器列表
  23. for (Advisor advisor : config.getAdvisors()) {
  24. if (advisor instanceof PointcutAdvisor) {
  25. PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
  26. /*
  27. * 调用 ClassFilter 对 bean 类型进行匹配,无法匹配则说明当前通知器
  28. * 不适合应用在当前 bean 上
  29. */
  30. if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
  31. // 将 advisor 中的 advice 转成相应的拦截器
  32. MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
  33. MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
  34. // 通过方法匹配器对目标方法进行匹配
  35. if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
  36. // 若 isRuntime 返回 true,则表明 MethodMatcher 要在运行时做一些检测
  37. if (mm.isRuntime()) {
  38. for (MethodInterceptor interceptor : interceptors) {
  39. interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
  40. }
  41. }
  42. else {
  43. interceptorList.addAll(Arrays.asList(interceptors));
  44. }
  45. }
  46. }
  47. }
  48. else if (advisor instanceof IntroductionAdvisor) {
  49. IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
  50. // IntroductionAdvisor 类型的通知器,仅需进行类级别的匹配即可
  51. if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
  52. Interceptor[] interceptors = registry.getInterceptors(advisor);
  53. interceptorList.addAll(Arrays.asList(interceptors));
  54. }
  55. }
  56. else {
  57. Interceptor[] interceptors = registry.getInterceptors(advisor);
  58. interceptorList.addAll(Arrays.asList(interceptors));
  59. }
  60. }
  61. return interceptorList;
  62. }
  63. public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
  64. List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);
  65. Advice advice = advisor.getAdvice();
  66. /*
  67. * 若 advice 是 MethodInterceptor 类型的,直接添加到 interceptors 中即可。
  68. * 比如 AspectJAfterAdvice 就实现了 MethodInterceptor 接口
  69. */
  70. if (advice instanceof MethodInterceptor) {
  71. interceptors.add((MethodInterceptor) advice);
  72. }
  73. /*
  74. * 对于 AspectJMethodBeforeAdvice 等类型的通知,由于没有实现 MethodInterceptor
  75. * 接口,所以这里需要通过适配器进行转换
  76. */
  77. for (AdvisorAdapter adapter : this.adapters) {
  78. if (adapter.supportsAdvice(advice)) {
  79. interceptors.add(adapter.getInterceptor(advisor));
  80. }
  81. }
  82. if (interceptors.isEmpty()) {
  83. throw new UnknownAdviceTypeException(advisor.getAdvice());
  84. }
  85. return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
  86. }

以上就是获取拦截器的过程,代码有点长,不过好在逻辑不是很复杂。这里简单总结一下以上源码的执行过程,如下:

  1. 从缓存中获取当前方法的拦截器链
  2. 若缓存未命中,则调用 getInterceptorsAndDynamicInterceptionAdvice 获取拦截器链
  3. 遍历通知器列表
  4. 对于 PointcutAdvisor 类型的通知器,这里要调用通知器所持有的切点(Pointcut)对类和方法进行匹配,匹配成功说明应向当前方法织入通知逻辑
  5. 调用 getInterceptors 方法对非 MethodInterceptor 类型的通知进行转换
  6. 返回拦截器数组,并在随后存入缓存中

这里需要说明一下,部分通知器是没有实现 MethodInterceptor 接口的,比如 AspectJMethodBeforeAdvice。我们可以看一下前置通知适配器是如何将前置通知转为拦截器的,如下:

  1. class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
  2. @Override
  3. public boolean supportsAdvice(Advice advice) {
  4. return (advice instanceof MethodBeforeAdvice);
  5. }
  6. @Override
  7. public MethodInterceptor getInterceptor(Advisor advisor) {
  8. MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
  9. // 创建 MethodBeforeAdviceInterceptor 拦截器
  10. return new MethodBeforeAdviceInterceptor(advice);
  11. }
  12. }

如上,适配器的逻辑比较简单,这里就不多说了。

现在我们已经获得了拦截器链,那接下来要做的事情就是启动拦截器了。所以接下来,我们一起去看看 Sring 是如何让拦截器链运行起来的。

3.3 启动拦截器链

3.3.1 执行拦截器链

本节的开始,我们先来说说 ReflectiveMethodInvocation。ReflectiveMethodInvocation 贯穿于拦截器链执行的始终,可以说是核心。该类的 proceed 方法用于启动启动拦截器链,下面我们去看看这个方法的逻辑。

  1. public class ReflectiveMethodInvocation implements ProxyMethodInvocation {
  2. private int currentInterceptorIndex = -1;
  3. public Object proceed() throws Throwable {
  4. // 拦截器链中的最后一个拦截器执行完后,即可执行目标方法
  5. if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
  6. // 执行目标方法
  7. return invokeJoinpoint();
  8. }
  9. Object interceptorOrInterceptionAdvice =
  10. this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
  11. if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
  12. InterceptorAndDynamicMethodMatcher dm =
  13. (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
  14. /*
  15. * 调用具有三个参数(3-args)的 matches 方法动态匹配目标方法,
  16. * 两个参数(2-args)的 matches 方法用于静态匹配
  17. */
  18. if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
  19. // 调用拦截器逻辑
  20. return dm.interceptor.invoke(this);
  21. }
  22. else {
  23. // 如果匹配失败,则忽略当前的拦截器
  24. return proceed();
  25. }
  26. }
  27. else {
  28. // 调用拦截器逻辑,并传递 ReflectiveMethodInvocation 对象
  29. return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
  30. }
  31. }
  32. }

如上,proceed 根据 currentInterceptorIndex 来确定当前应执行哪个拦截器,并在调用拦截器的 invoke 方法时,将自己作为参数传给该方法。前面的章节中,我们看过了前置拦截器的源码,这里来看一下后置拦截器源码。如下:

  1. public class AspectJAfterAdvice extends AbstractAspectJAdvice
  2. implements MethodInterceptor, AfterAdvice, Serializable {
  3. public AspectJAfterAdvice(
  4. Method aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) {
  5. super(aspectJBeforeAdviceMethod, pointcut, aif);
  6. }
  7. @Override
  8. public Object invoke(MethodInvocation mi) throws Throwable {
  9. try {
  10. // 调用 proceed
  11. return mi.proceed();
  12. }
  13. finally {
  14. // 调用后置通知逻辑
  15. invokeAdviceMethod(getJoinPointMatch(), null, null);
  16. }
  17. }
  18. //...
  19. }

如上,由于后置通知需要在目标方法返回后执行,所以 AspectJAfterAdvice 先调用 mi.proceed() 执行下一个拦截器逻辑,等下一个拦截器返回后,再执行后置通知逻辑。如果大家不太理解的话,先看个图。这里假设目标方法 method 在执行前,需要执行两个前置通知和一个后置通知。下面我们看一下由三个拦截器组成的拦截器链是如何执行的,如下:



注:这里用 advice.after() 表示执行后置通知

本节的最后,插播一个拦截器,即 ExposeInvocationInterceptor。为啥要在这里介绍这个拦截器呢,原因是我在Spring AOP 源码分析 - 筛选合适的通知器一文中,在介绍 extendAdvisors 方法时,有一个点没有详细说明。现在大家已经知道拦截器的概念了,就可以把之前没法详细说明的地方进行补充说明。这里再贴一下 extendAdvisors 方法的源码,如下:

  1. protected void extendAdvisors(List<Advisor> candidateAdvisors) {
  2. AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(candidateAdvisors);
  3. }
  4. public static boolean makeAdvisorChainAspectJCapableIfNecessary(List<Advisor> advisors) {
  5. if (!advisors.isEmpty()) {
  6. // 省略部分代码
  7. if (foundAspectJAdvice && !advisors.contains(ExposeInvocationInterceptor.ADVISOR)) {
  8. // 向通知器列表中添加 ExposeInvocationInterceptor.ADVISOR
  9. advisors.add(0, ExposeInvocationInterceptor.ADVISOR);
  10. return true;
  11. }
  12. }
  13. return false;
  14. }

如上,extendAdvisors 所调用的方法会向通知器列表首部添加 ExposeInvocationInterceptor.ADVISOR。现在我们再来看看 ExposeInvocationInterceptor 的源码,如下:

  1. public class ExposeInvocationInterceptor implements MethodInterceptor, PriorityOrdered, Serializable {
  2. public static final ExposeInvocationInterceptor INSTANCE = new ExposeInvocationInterceptor();
  3. // 创建 DefaultPointcutAdvisor 匿名对象
  4. public static final Advisor ADVISOR = new DefaultPointcutAdvisor(INSTANCE) {
  5. @Override
  6. public String toString() {
  7. return ExposeInvocationInterceptor.class.getName() +".ADVISOR";
  8. }
  9. };
  10. private static final ThreadLocal<MethodInvocation> invocation =
  11. new NamedThreadLocal<MethodInvocation>("Current AOP method invocation");
  12. public static MethodInvocation currentInvocation() throws IllegalStateException {
  13. MethodInvocation mi = invocation.get();
  14. if (mi == null)
  15. throw new IllegalStateException(
  16. "No MethodInvocation found: Check that an AOP invocation is in progress, and that the " +
  17. "ExposeInvocationInterceptor is upfront in the interceptor chain. Specifically, note that " +
  18. "advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor!");
  19. return mi;
  20. }
  21. // 私有构造方法
  22. private ExposeInvocationInterceptor() {
  23. }
  24. @Override
  25. public Object invoke(MethodInvocation mi) throws Throwable {
  26. MethodInvocation oldInvocation = invocation.get();
  27. // 将 mi 设置到 ThreadLocal 中
  28. invocation.set(mi);
  29. try {
  30. // 调用下一个拦截器
  31. return mi.proceed();
  32. }
  33. finally {
  34. invocation.set(oldInvocation);
  35. }
  36. }
  37. //...
  38. }

如上,ExposeInvocationInterceptor.ADVISOR 经过 registry.getInterceptors 方法(前面已分析过)处理后,即可得到 ExposeInvocationInterceptor。ExposeInvocationInterceptor 的作用是用于暴露 MethodInvocation 对象到 ThreadLocal 中,其名字也体现出了这一点。如果其他地方需要当前的 MethodInvocation 对象,直接通过调用 currentInvocation 方法取出。至于哪些地方需要 MethodInvocation,这个大家自己去探索吧。最后,建议大家写点代码调试一下。我在一开始阅读代码时,并没有注意到 ExposeInvocationInterceptor,而是在调试代码的过程中才发现的。比如:

好了,关于拦截器链的执行过程这里就讲完了。下一节,我们来看一下目标方法的执行过程。大家再忍忍,源码很快分析完了。

3.3.2 执行目标方法

与前面的大部头相比,本节的源码比较短,也很简单。本节我们来看一下目标方法的执行过程,如下:

  1. protected Object invokeJoinpoint() throws Throwable {
  2. return AopUtils.invokeJoinpointUsingReflection(this.target, this.method, this.arguments);
  3. }
  4. public abstract class AopUtils {
  5. public static Object invokeJoinpointUsingReflection(Object target, Method method, Object[] args)
  6. throws Throwable {
  7. try {
  8. ReflectionUtils.makeAccessible(method);
  9. // 通过反射执行目标方法
  10. return method.invoke(target, args);
  11. }
  12. catch (InvocationTargetException ex) {...}
  13. catch (IllegalArgumentException ex) {...}
  14. catch (IllegalAccessException ex) {...}
  15. }
  16. }

目标方法时通过反射执行的,比较简单的吧。好了,就不多说了,over。

4.总结

到此,本篇文章的就要结束了。本篇文章是Spring AOP 源码分析系列文章的最后一篇,从阅读源码到写完本系列的4篇文章总共花了约两周的时间。总的来说还是有点累的,但是也有很大的收获和成就感,值了。需要说明的是,Spring IOC 和 AOP 部分的源码我分析的并不是非常详细,也有很多地方没弄懂。这一系列的文章,是作为自己工作两年的一个总结。由于工作时间不长,工作经验和技术水平目前都还处于入门阶段。所以暂时很难把 Spring IOC 和 AOP 模块的源码分析的很出彩,这个请见谅。如果大家在阅读文章的过程中发现了错误,可以指出来,也希望多多指教,这里先说说谢谢。

好了,本篇文章到这里就结束了。谢谢大家的阅读。

参考

附录:Spring 源码分析文章列表

Ⅰ. IOC

更新时间 标题
2018-05-30 Spring IOC 容器源码分析系列文章导读
2018-06-01 Spring IOC 容器源码分析 - 获取单例 bean
2018-06-04 Spring IOC 容器源码分析 - 创建单例 bean 的过程
2018-06-06 Spring IOC 容器源码分析 - 创建原始 bean 对象
2018-06-08 Spring IOC 容器源码分析 - 循环依赖的解决办法
2018-06-11 Spring IOC 容器源码分析 - 填充属性到 bean 原始对象
2018-06-11 Spring IOC 容器源码分析 - 余下的初始化工作

Ⅱ. AOP

更新时间 标题
2018-06-17 Spring AOP 源码分析系列文章导读
2018-06-20 Spring AOP 源码分析 - 筛选合适的通知器
2018-06-20 Spring AOP 源码分析 - 创建代理对象
2018-06-22 Spring AOP 源码分析 - 拦截器链的执行过程

Ⅲ. MVC

更新时间 标题
2018-06-29 Spring MVC 原理探秘 - 一个请求的旅行过程
2018-06-30 Spring MVC 原理探秘 - 容器的创建过程

本文在知识共享许可协议 4.0 下发布,转载需在明显位置处注明出处

作者:田小波

本文同步发布在我的个人博客:http://www.tianxiaobo.com


本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。

Spring AOP 源码分析 - 拦截器链的执行过程的更多相关文章

  1. Spring AOP 源码分析 - 筛选合适的通知器

    1.简介 从本篇文章开始,我将会对 Spring AOP 部分的源码进行分析.本文是 Spring AOP 源码分析系列文章的第二篇,本文主要分析 Spring AOP 是如何为目标 bean 筛选出 ...

  2. Spring AOP 源码分析 - 创建代理对象

    1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...

  3. Spring AOP 源码分析系列文章导读

    1. 简介 前一段时间,我学习了 Spring IOC 容器方面的源码,并写了数篇文章对此进行讲解.在写完 Spring IOC 容器源码分析系列文章中的最后一篇后,没敢懈怠,趁热打铁,花了3天时间阅 ...

  4. spring AOP源码分析(三)

    在上一篇文章 spring AOP源码分析(二)中,我们已经知道如何生成一个代理对象了,那么当代理对象调用代理方法时,增强行为也就是拦截器是如何发挥作用的呢?接下来我们将介绍JDK动态代理和cglib ...

  5. Spring AOP源码分析(三):基于JDK动态代理和CGLIB创建代理对象的实现原理

    AOP代理对象的创建 AOP相关的代理对象的创建主要在applyBeanPostProcessorsBeforeInstantiation方法实现: protected Object applyBea ...

  6. 5.2 Spring5源码--Spring AOP源码分析二

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  7. 5.2 spring5源码--spring AOP源码分析二--切面的配置方式

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  8. Spring AOP 源码分析

    一.准备工作 在这里我先简单记录下如何实现一个aop:   AOP:[动态代理] 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式: 1.导入aop模块:Spring AOP: ...

  9. spring aop 源码分析(三) @Scope注解创建代理对象

    一.源码环境的搭建: @Component @Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON,proxyMode = ScopedP ...

随机推荐

  1. Confluence 6 降级你的许可证

    如果你决定降级你 Confluence 的许可证而削减你的许可证开支,你需要确定当前已经直排的用户许可证数量(在用户许可证页面中)要少于你希望应用的新的许可证的允许用户数量,在你应用新许可证的时候. ...

  2. javaScript遍历对象、数组总结

        javaScript遍历对象总结 1.使用Object.keys()遍历 返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含Symbol属性). var obj = {'0':'a ...

  3. LeetCode(107): 二叉树的层次遍历 II

    Easy! 题目描述: 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,20,null,null,15,7], ...

  4. day4-list,列表

    dist: 增:1.append(obj),在列表最后添加元素: 2.insert(index,object),在索引处添加元素: 3.extend,迭代添加元素,所添加元素必须可迭代. 删:1.po ...

  5. Linux基础二:初识linux命令

    一.UNIX和Linux操作系统概述 1.UNIX是什么 1)UNIX的定义: UNIX是一个计算机操作系统,一个用来协调.管理和控制计算机硬件和软件资源的控制程序. 2)UNIX操作系统的特点:多用 ...

  6. 浏览器URL中 encodeURIComponent()加密和decodeURIComponent()解码

    encodeURIComponent()加密 定义和用法 encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. 语法 encodeURIComponent(URIstr ...

  7. 解决beego中同时开启http和https时,https端口占用问题

    在beego的app.go文件中, 找到 // run normal mode if BConfig.Listen.EnableHTTPS { go func() { time.Sleep( * ti ...

  8. 用servlet打内容到网页上

    关键代码 response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWrite ...

  9. Ajax增删改查-----------删 改

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. JavaBean toString() - 将bean对象打印成字符串

    JavaBean toString方式 https://www.cnblogs.com/thiaoqueen/p/7086195.html //方法一:自动生成 @Override public St ...