Preface:
This article will provide a deep understanding and learning through serialization.SpringRelated core technologies and implementation principles. IncludingSpringOfBeanLifecycle management, declarative transaction management,AOPManagement, annotation application implementation, andRelated Applications that are extended on it.
This article introducesSpring AOP.
Let's take a lookAOPRelated Concepts:
1)Aspect (Section): Indicates that the abstract of a cross-cutting concern is a cut. Similar to the class, the focus of the two is different. The class is the abstraction of Object Features, while the aspect is the abstraction of cross-cutting concerns.
2)Joinpoint (Connection Point): Points intercepted. InSpringThese points refer to methods, becauseSpringOnly connection points of the method type are supported. In fact, the connection points can also beFieldOr class constructor.
3)Advice (Notification): Refers to the things to be done after interception to the connection point. They are divided into pre-notification, post-notification, exception notification, final notification, and surround notification.
4)Pointcut (Entry Point): Refers toJointpointDefinition of interception.
5)Target (Target object): The target object to be implemented by the proxy.
6)Weave (Woven): RefersAspectsApplyTargetObject and causeProxyObject creation process.
7)Introduction (Introduction): Without modifying the class code,IntroductionYou can dynamically add some methods or attributes for the class during runtime.
SpringOfAOPPowerful configuration functions, such as automatic proxy(Autoproxy), ProvideXMLAnd annotation configuration method. No matter which configuration is supported, its final nature is throughJdkDynamicAopProxyOrCglibProxyFactoryCompleted objectAOPSupports interception.
One of the following configuration methods is used:(Based onBeanName auto-Proxy Support)To explainSpring AOPImplementation Principle:
The following is a configuration example::
BeanNameAutoProxyCreatorString or wildcardBeanAutomatic CreationAOPProxy.
Below isBeanNameAutoProxyCreatorClass graph, which implementsBeanPostProcessorInterface.
Introduction in the previous chapterSpring BeanIntroduction to Lifecycle ManagementBeanPostProcessorYesSpringInBeanBefore and after the object is instantiated, the corresponding method is called back.
SoBeanNameAutoProxyCreatorIs IMPLEMENTEDPostProcessBeforeInstantiationMethodBeanBefore the object is implementedAOP.
The core code is as follows::
In the code, it will callCreateProxyMethod To implement automatic code encapsulation of object instances.
Analyze the code: CreateProxyThe method is actually called.SpringOfProxyFactoryProxy encapsulation implementation for objects
The main implementation code is marked below:
NextAopProxyFactoryYesSpringProvided interface class,SpringDefaultDefaultAopProxyFactoryImplementation class.
Let's analyze it again.CreateAopProxyMethod implementation,The main implementation Code marked below
Note: We found the above CodeSpringForBeanObjectAOPTwo methods are used for processing. If there is an interface classJDKDynamicAopProxyIf it is a common class, useCglibProxyFacotry. AboutCglibFor details about this project, refer to the instructions on its official website.