一、关键图

image.png

二、寻找代理方法

BeanPostProcessor 的执行位置 https://www.yuque.com/wangchao-volk4/fdw9ek/vf0eas#Vyviy

BeanPostProcessor 的一个实现类 AbstractAutoProxyCreator 中的方法 postProcessAfterInitialization
image.png
关注 BeanPostProcessor 的两个方法,一个 before 一个 after

  1. public interface BeanPostProcessor {
  2. /**
  3. * Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
  4. * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
  5. * or a custom init-method). The bean will already be populated with property values.
  6. * The returned bean instance may be a wrapper around the original.
  7. * <p>The default implementation returns the given {@code bean} as-is.
  8. * @param bean the new bean instance
  9. * @param beanName the name of the bean
  10. * @return the bean instance to use, either the original or a wrapped one;
  11. * if {@code null}, no subsequent BeanPostProcessors will be invoked
  12. * @throws org.springframework.beans.BeansException in case of errors
  13. * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
  14. */
  15. @Nullable
  16. default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  17. return bean;
  18. }
  19. /**
  20. * Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
  21. * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
  22. * or a custom init-method). The bean will already be populated with property values.
  23. * The returned bean instance may be a wrapper around the original.
  24. * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
  25. * instance and the objects created by the FactoryBean (as of Spring 2.0). The
  26. * post-processor can decide whether to apply to either the FactoryBean or created
  27. * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
  28. * <p>This callback will also be invoked after a short-circuiting triggered by a
  29. * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
  30. * in contrast to all other {@code BeanPostProcessor} callbacks.
  31. * <p>The default implementation returns the given {@code bean} as-is.
  32. * @param bean the new bean instance
  33. * @param beanName the name of the bean
  34. * @return the bean instance to use, either the original or a wrapped one;
  35. * if {@code null}, no subsequent BeanPostProcessors will be invoked
  36. * @throws org.springframework.beans.BeansException in case of errors
  37. * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
  38. * @see org.springframework.beans.factory.FactoryBean
  39. */
  40. @Nullable
  41. default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  42. return bean;
  43. }
  44. }

image.png
进入 方法 wrapIfNecessary 中的 createProxy 方法。找到最后的proxyFactory.getProxy(_getProxyClassLoader())
image.png
进入 _getProxy 方法,找到 aopProxy接口
image.png

三、总结

1、Sping 的aop 过程在对象实例化阶段,即 beanPostProcessor