AopNamespaceHandler

AOP源码分析 - 图1

AspectJAutoProxyBeanDefinitionParser

  1. class AspectJAutoProxyBeanDefinitionParser implements BeanDefinitionParser {
  2. AspectJAutoProxyBeanDefinitionParser() {
  3. }
  4. @Nullable
  5. public BeanDefinition parse(Element element, ParserContext parserContext) {
  6. //注册AspectJAnnotationAutoProxyCreator
  7. AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);
  8. //对于注解中的子类的处理
  9. this.extendBeanDefinition(element, parserContext);
  10. return null;
  11. }
  12. }

AbstractAutoProxyCreator

  1. public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
  2. //初始化前处理
  3. public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
  4. Object cacheKey = this.getCacheKey(beanClass, beanName);
  5. if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
  6. if (this.advisedBeans.containsKey(cacheKey)) {
  7. return null;
  8. }
  9. if (this.isInfrastructureClass(beanClass) || this.shouldSkip(beanClass, beanName)) {
  10. this.advisedBeans.put(cacheKey, Boolean.FALSE);
  11. return null;
  12. }
  13. }
  14. TargetSource targetSource = this.getCustomTargetSource(beanClass, beanName);
  15. if (targetSource != null) {
  16. if (StringUtils.hasLength(beanName)) {
  17. this.targetSourcedBeans.add(beanName);
  18. }
  19. Object[] specificInterceptors = this.getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
  20. Object proxy = this.createProxy(beanClass, beanName, specificInterceptors, targetSource);
  21. this.proxyTypes.put(cacheKey, proxy.getClass());
  22. return proxy;
  23. } else {
  24. return null;
  25. }
  26. }
  27. //初始化后处理
  28. public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
  29. if (bean != null) {
  30. Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
  31. if (this.earlyProxyReferences.remove(cacheKey) != bean) {
  32. //如果适合被代理,封装指定bean
  33. return this.wrapIfNecessary(bean, beanName, cacheKey);
  34. }
  35. }
  36. return bean;
  37. }
  38. protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
  39. //已经处理过
  40. if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
  41. return bean;
  42. //无需增强
  43. } else if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
  44. return bean;
  45. //创建代理对象
  46. } else if (!this.isInfrastructureClass(bean.getClass()) && !this.shouldSkip(bean.getClass(), beanName)) {
  47. //如果存在增强方法则创建代理
  48. Object[] specificInterceptors = this.getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, (TargetSource)null);
  49. if (specificInterceptors != DO_NOT_PROXY) {
  50. //保存需要增强的bean
  51. this.advisedBeans.put(cacheKey, Boolean.TRUE);
  52. //创建代理
  53. Object proxy = this.createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
  54. this.proxyTypes.put(cacheKey, proxy.getClass());
  55. return proxy;
  56. } else {
  57. this.advisedBeans.put(cacheKey, Boolean.FALSE);
  58. return bean;
  59. }
  60. } else {
  61. this.advisedBeans.put(cacheKey, Boolean.FALSE);
  62. return bean;
  63. }
  64. }
  65. //创建代理对象
  66. protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) {
  67. if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
  68. AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory)this.beanFactory, beanName, beanClass);
  69. }
  70. ProxyFactory proxyFactory = new ProxyFactory();
  71. proxyFactory.copyFrom(this);
  72. if (!proxyFactory.isProxyTargetClass()) {
  73. if (this.shouldProxyTargetClass(beanClass, beanName)) {
  74. proxyFactory.setProxyTargetClass(true);
  75. } else {
  76. this.evaluateProxyInterfaces(beanClass, proxyFactory);
  77. }
  78. }
  79. //获取增强器
  80. Advisor[] advisors = this.buildAdvisors(beanName, specificInterceptors);
  81. //加入增强器
  82. proxyFactory.addAdvisors(advisors);
  83. //设置要代理的类
  84. proxyFactory.setTargetSource(targetSource);
  85. this.customizeProxyFactory(proxyFactory);
  86. proxyFactory.setFrozen(this.freezeProxy);
  87. if (this.advisorsPreFiltered()) {
  88. proxyFactory.setPreFiltered(true);
  89. }
  90. return proxyFactory.getProxy(this.getProxyClassLoader());
  91. }
  92. }

AbstractAdvisorAutoProxyCreator

  1. public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator {
  2. @Nullable
  3. protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {
  4. ////获取增强方法
  5. List<Advisor> advisors = this.findEligibleAdvisors(beanClass, beanName);
  6. return advisors.isEmpty() ? DO_NOT_PROXY : advisors.toArray();
  7. }
  8. }