Spring-AOP source analysis with the Notes (a)

1.@EnableAspectJAutoProxy(proxyTargetClass = true)

Is broke "org.springframework.aop.config.internalAutoProxyCreator" :: AnnotationAwareAspectJAutoProxyCreator.class the Bean into a container

2. Analysis of class structure AnnotationAwareAspectJAutoProxyCreator.class

What it is important to draw a few.

3. Review AnnotationAwareAspectJAutoProxyCreator source

= "View parent AbstractAutoProxyCreator, etc. There are various methods of setBeanFactory inherited

4 is a graph collated meal (???) below

pic1-> AOP code analysis and FIG.

image

5. Analyze AbstractAutoProxyCreator.

There setBeanFactory O flag is rewritten to open, there is a method initBeanFactory, open, create a advisorRetrievalHelper cut tool, but also to rewrite O created aspectJAdvisorsBuilder

6.pic1 Turi is evident there are two key methods:

postProcessBeforeInstantiation和postProcessAfterInitialization

Apparently before and after the processing logic. Certainly look before logic postProcessBeforeInstantiation ah

7.postProcessBeforeInstantiation

Break away from, actually go from here

image

image

So this analysis before the IOC createBean in the resolveBeforeInstantiation is the key to AOP implementation.

8. Analysis of postProcessBeforeInstantiation details

8.1 isInfrastructureClass determine whether the underlying Bean

就是那种AOP本身的比如Advice、Pointcut、AopInfrastructureBean等,基础的这种直接返回不需要代理

8.2 shouldSkip是否要跳过(包含找出切面)

8.2.1找出增强器(那种切面里的@After、@Before的方法)

跟随断点跳跃到之前setBeanFactory创建的advisorRetrievalHelper里去了帮忙实现的找增强器

实际上这里面又分两部分了,一个是找事务的(findCandidateAdvisors)(本次断点容器和缓存中都就没用了直接返回空,不是事务,具体可以看aop源码instan...图)

另一部分是this.aspectJAdvisorsBuilder.buildAspectJAdvisors()

image

跳进去看了下,它把所有的beanname拿出来找,去找切面类 找到了我写的MyLogAspect

(打了切面切面注解)

然后找到了切面类,调用相关封装的getAdvisors方法去拿增强器了。

其实就是获取切面类的所有方法(getAdvisorMethods)把每个包含增强那些注解的方法包装成Advice 放进缓存。

protected static AspectJAnnotation<?> findAspectJAnnotationOnMethod(Method method) {
   for (Class<?> clazz : ASPECTJ_ANNOTATION_CLASSES) {
      AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) clazz);
      if (foundAnnotation != null) {
         return foundAnnotation;
      }
   }
   return null;
}

private static final Class<?>[] ASPECTJ_ANNOTATION_CLASSES = new Class<?>[] {
      Pointcut.class, Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class};

然后

switch (aspectJAnnotation.getAnnotationType()) {
   case AtPointcut:
      if (logger.isDebugEnabled()) {
         logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
      }
      return null;
   case AtAround:
      springAdvice = new AspectJAroundAdvice(
            candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
      break;
   case AtBefore:
      springAdvice = new AspectJMethodBeforeAdvice(
            candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
      break;
   case AtAfter:
      springAdvice = new AspectJAfterAdvice(
            candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
      break;
   case AtAfterReturning:
      springAdvice = new AspectJAfterReturningAdvice(
            candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
      AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
      if (StringUtils.hasText(afterReturningAnnotation.returning())) {
         springAdvice.setReturningName(afterReturningAnnotation.returning());
      }
      break;
   case AtAfterThrowing:
      springAdvice = new AspectJAfterThrowingAdvice(
            candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
      AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
      if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
         springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
      }
      break;
   default:
      throw new UnsupportedOperationException(
            "Unsupported advice type on method: " + candidateAdviceMethod);
}

switch判断注解类型 创建不同类型Advice 放进缓存

最终由this.aspectJAdvisorsBuilder.buildAspectJAdvisors()找到了我写的所有切面的增强方法

最终把整个切面类放到了advisorsCache的ConcurrentHashMap,以切面类的beanname作为key,增强器方法advice集合作为value

9.postProcessAfterInitialization

条件断点,当beanname = "calculate"时停住。找出增强器后,看看后续aop是怎么和目标类结合的。

10.postProcessAfterInitialization细节

断点已达。老办法,看看是从哪调来的:

image

image

原来是这里,ioc创建完对象并给属性赋完值之后。

Back postProcessAfterInitialization itself, on the transfer of a wrapIfNecessary. Logic in it

10.1 find the corresponding booster

Open breakpoint continue to be a major getAdvicesAndAdvisorsForBean then transferred findEligibleAdvisors then ran to find that previous transactions slightly. Then mainly findAdvisorsThatCanApply (also see the name, you can use this bean corresponding section), but also into a canApply (candidate, clazz, hasIntroductions), which is a specific judgments:

image

image

Get all methods, with a matcher match to match on the match

Write down cycle, all to find out, find enhancer is over!

Generating a proxy object weaving 10.2

Back wrapIfNecessary begin to create a proxy object:

image

image

Inside this box up involving annotation configuration corresponding to your configuration:

@EnableAspectJAutoProxy(exposeProxy = true)

@EnableAspectJAutoProxy(proxyTargetClass = true)

If these two sets, it would deal with implementation.

The second priority is to start cglib, the first one is to create a good agent after exposed to the object, so that we can so that the original method would have been to call this.dadd situation can not be enhanced by

((Calculate) AopContext.currentProxy ()) add (numA, numB);. Also trigger enhancements

Behind the words is to create a proxy factory ProxyFactory, the booster set in, and finally call getProxy

Jump all the way:

image

This is the last thing the Internet back back back up. In no interface and did not play, then create a priority cglib otherwise cglib created with jdk.

problem:

1. how get out of the inheritance structure which is important and what is unimportant not need to see

2. how sorted out?

Guess you like

Origin www.cnblogs.com/chz-blogs/p/12005744.html