AOP source code analysis and implementation Overview

AOP: [Dynamic] Agent:

 Means dynamically during program execution will be cut into a piece of code for running the specified location designation method of programming;

1, introducing aop module; the Spring the AOP: (Spring-Aspects)
 2, define a business logic class (MathCalculator); when the business logic operation will be log printing performed (before the method, the method ends running by abnormal, XXX)
 . 3 define a class cut log (LogAspects): class section which methods require dynamic sensing operation where MathCalculator.div then performed;

 

 Notification Method:
 Pre-notification (@Before): logStart: running before the target method (div) run
 after advice (@After): logEnd: running (whether normal or abnormal method ends after the end of the target method (div) run )
 returns the notification (@AfterReturning): logReturn: run after the target method (div) returns normally
 exception notification (@AfterThrowing): logException: after abnormal operation occurs in the target method (div)
 around advice (@Around): dynamic agent, manual further the objectives of the operating method (joinPoint.procced ())

 4, a method to cut the target class label run anytime (notification annotations);

 5, the cut type and business logic classes (class where the target method) are added to the vessel;

 6, Spring must tell which class is class section (the section to add an annotation class: the @Aspect)
 [7], to Canada @EnableAspectJAutoProxy class configuration [aop open annotation-based model]

 In Spring many @EnableXXX;

 three steps:
 1), and cut the business logic components are added to the vessel classes; tell Spring which is cut class (@Aspect)
 2), a notification method on each section of the class labeling notification annotation, Spring told when and where to run (pointcut expression)
 3) to open the annotation-based aop mode; @EnableAspectJAutoProxy

 AOP principle: [container to see what components are registered, the work of this component when this component What function? ]
 @EnableAspectJAutoProxy;
 1, what @ EnableAspectJAutoProxy that?
 @Import (AspectJAutoProxyRegistrar.class): AspectJAutoProxyRegistrar introduced to the vessel
 using custom registered AspectJAutoProxyRegistrar container to the bean; BeanDefinetion
 internalAutoProxyCreator = AnnotationAwareAspectJAutoProxyCreator

 to register a container AnnotationAwareAspectJAutoProxyCreator;

 2, AnnotationAwareAspectJAutoProxyCreator:
 AnnotationAwareAspectJAutoProxyCreator
 -> AspectJAwareAdvisorAutoProxyCreator
 -> AbstractAdvisorAutoProxyCreator
 -> AbstractAutoProxyCreator
 the implements SmartInstantiationAwareBeanPostProcessor, the BeanFactoryAware
 concerns post-processor (do something before and after initialization is complete bean), automated assembly the BeanFactory

 AbstractAutoProxyCreator.setBeanFactory ()
 AbstractAutoProxyCreator logical postprocessor;.

 AbstractAdvisorAutoProxyCreator.setBeanFactory () - " initBeanFactory ()

 AnnotationAwareAspectJAutoProxyCreator.initBeanFactory ()


 process:
 1), the incoming configuration class, create ioc container
 2), the configuration register class calls the refresh () refresh container;
 . 3), registerBeanPostProcessors (beanFactory); post-processor register bean to facilitate interception create the bean;
 1), first get ioc container is already defined all BeanPostProcessor need to create objects
 2), to add another container BeanPostProcessor
 3), the Sunrise realized BeanPostProcessor PriorityOrdered interface;
 4), then to achieve a vessel registered BeanPostProcessor Ordered interface;
 5), priority registration did not realize BeanPostProcessor interface;
 6), registered BeanPostProcessor, in fact, is to create BeanPostProcessor objects stored in the container;
 

The created internalAutoProxyCreator BeanPostProcessor AnnotationAwareAspectJAutoProxyCreator] [
 1), create an instance of the Bean
 2), populateBean; assigned to the various properties of the bean
 3), initializeBean: initializing bean;
 1), invokeAwareMethods (): method for processing a callback interface Aware
 2), applyBeanPostProcessorsBeforeInitialization (): postProcessBeforeInitialization application postprocessor ()
 . 3), invokeInitMethods (); performs self-initialization method defined in
 4), applyBeanPostProcessorsAfterInitialization (); postProcessAfterInitialization performed postprocessor ();
 . 4), the BeanPostProcessor (AnnotationAwareAspectJAutoProxyCreator) successfully created; - "aspectJAdvisorsBuilder
 7), the BeanPostProcessor registered to the BeanFactory;
 ; beanFactory.addBeanPostProcessor (PostProcessor)
 process ======= These are created and registered AnnotationAwareAspectJAutoProxyCreator of ========

 = AnnotationAwareAspectJAutoProxyCreator> InstantiationAwareBeanPostProcessor
 . 4), finishBeanFactoryInitialization (beanFactory); BeanFactory completed initialization; create the remaining single instance the bean
 . 1), traversing the container acquires all Bean, in turn creating an object the getBean (the beanName);
 getBean-> doGetBean () - > getSingleton () ->
 2), to create a bean
 [AnnotationAwareAspectJAutoProxyCreator have a bean to intercept before all creation, InstantiationAwareBeanPostProcessor, calls postProcessBeforeInstantiation ()]
 1), to obtain the current bean existing cache, if you can get to, indicating that bean before being created through the direct use, or re-create;
 just create a good bean will be cached
 2), createBean (); create bean;
 AnnotationAwareAspectJAutoProxyCreator will first attempt to return to the bean instance before any bean create
 [BeanPostProcessor in bean objects created before and after the initialization call]
 [InstantiationAwareBeanPostProcessor processor is set to return after the first try of the object before creating a Bean instance]
 1), resolveBeforeInstantiation (beanName, mbdToUse ); parsing BeforeInstantiation
 desired postprocessor herein can return a proxy object; if the agent can return the object to use, if not to continue to
 1), the first post-processor attempts to return the object;
 the bean = applyBeanPostProcessorsBeforeInstantiation ():
 get all the post-processor, if InstantiationAwareBeanPostProcessor;
 implementation postProcessBeforeInstantiation
 IF (the bean = null!) {
the bean = applyBeanPostProcessorsAfterInitialization (the bean, the beanName);
}

 2), doCreateBean (the beanName, mbdToUse, args); true to create a bean instance; and 3.6 processes the same;
 3),


 AnnotationAwareAspectJAutoProxyCreator [InstantiationAwareBeanPostProcessor] role:
 before 1), each bean is created, call postProcessBeforeInstantiation ();
 care MathCalculator and LogAspect create
 1) to determine the current bean is in advisedBeans in (save all the need to enhance the bean)
 2), it is determined whether the current is the basis of the type of bean Advice, Pointcut, Advisor, AopInfrastructureBean,
 or whether it is cut (@Aspect)
 . 3), whether to skip
 a) obtaining a candidate enhancer (section inside the notification method) [ List <Advisor> candidateAdvisors]
 each package booster notification method is InstantiationModelAwarePointcutAdvisor;
 determining whether each booster is AspectJPointcutAdvisor type; return to true
 2), always returns to false

 2), to create objects
 postProcessAfterInitialization;
 return wrapIfNecessary (the bean, the beanName , cacheKey); // if the package if necessary
 1) to acquire all of the enhancements currently the bean (notification method) Object [] specificInterceptors
 1, to find all of the booster candidate (which notice is required to find a method to cut the current bean method a)
 2, the booster can be used to obtain the bean.
 3, to enhance Sorting
 2), save the current bean in advisedBeans in;
 3), if the current bean needs to be enhanced, creating the current proxy object of the bean;
 1), to obtain all enhancer (notification method)
 2), save to the ProxyFactory
 3 ), create a proxy object: Spring automatic decision
 JdkDynamicAopProxy (config); jdk dynamic proxies;
 ObjenesisCglibAopProxy (config); dynamic proxies cglib; and
 4), the vessel is returned in the current component used cglib enhanced proxy object;
 5), after the container obtained is the agent of this component object when the execution of the target process, the proxy object executes the process of notifying method;


 3), the target method is performed;
 container holds components of the proxy object (the object after cglib enhancement), which holds the object details (such as enhancer, the target object, XXX);
 . 1), CglibAopProxy.intercept (); performing interception target method
 2), the target acquisition method of interceptor chains to be performed in accordance with the object ProxyFactory;
 List <Object> = this.advised.getInterceptorsAndDynamicInterceptionAdvice catena alberghiera (Method, targetClass);
 . 1), List <Object> 5 interceptorList save all interceptor
 a default ExposeInvocationInterceptor enhancer and 4;
 2), through all of the booster, which was converted interceptor;
 registry.getInterceptors (Advisor) ;
 3), the booster into List <Meth odInterceptor>;
 if MethodInterceptor, added directly into the collection
 If not, the booster using AdvisorAdapter into MethodInterceptor;
 conversion is complete return MethodInterceptor array;

 3), if there is no interceptor chain, direct execution of the target method;
 interceptor chain (each notification method has been packaged as a method of interceptor, the use of MethodInterceptor mechanism)
 4), if the interceptor chain, the target object need to be performed, the target method,
 the interceptor chain to pass information to create a CglibMethodInvocation object
 and call retVal = mi.proceed Object ();
 5), the interceptor chain triggering procedure;
 1), if not executed as interceptors and interceptors index array -1 size of the execution of the target method, or the interceptor (assigned to the last interceptor) performs the target method;
 2), each acquisition chain interception , a invoke method performs the interceptor, each interceptor interceptor wait for the next execution returns again after completion of execution;
 mechanism interceptor chain, to ensure the order of execution of the target method and the notification method;

 summarized:
 . 1), opening AOP function @EnableAspectJAutoProxy
 2), @EnableAspectJAutoProxy give container A registration component AnnotationAwareAspectJAutoProxyCreator
 . 3), a post-processor is AnnotationAwareAspectJAutoProxyCreator;
 4), the container creation process:
 1), registerBeanPostProcessors () register post processor; AnnotationAwareAspectJAutoProxyCreator create objects
 2), finishBeanFactoryInitialization () initializes the remaining single instance the bean
 . 1), and create a business logic component assembly section
 2), the process of creating AnnotationAwareAspectJAutoProxyCreator intercept module
 3), after assembly to create finished, it is determined whether the component needs to be enhanced
 are: notification method facets, packaged enhancer (Advisor); create a proxy object (CGLIB) to business logic component;
 5), execution of the target method:
 1), the proxy object execution target method
 2), CglibAopProxy.intercept ();
*. 1), to give the target method interceptor chain (booster packaged interceptor MethodInterceptor)
* 2), using the interceptor chain mechanism, change into each interceptors execution;
* 3), the effect of:
* normally performed: pre-notification - "target method - the" rear notice - "return notification
* abnormal: pre-notification -" target method - the "rear notice -" abnormality notification

 

Guess you like

Origin www.cnblogs.com/studygithub5208868/p/11300643.html