In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to carry out the structural analysis of the implementation of the Spring AOP framework, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Understand the SpringAOP framework from the perspective of implementation.
Angle of observation:
Understand the SpringAOP framework from four aspects: external interface, internal implementation, components and execution process.
The style of this article:
First list the basic concepts of AOP
Secondly, it introduces the list of core components involved in the framework and the structural relationship diagram between the components.
Then refine the part of the structure diagram
Then there is a simple sample
* * is the postscript.
Note:
1. The source code for this article is based on Spring2.x. The source code for Spring is also evolving, but it has little impact on the underlying code.
two。 This article is a sister post on the structural analysis of the Spring IoC container implementation.
Text:
Introduction to the basic concepts involved in the Spring AOP framework:
Concern: a concern can be a specific problem, concept, or application's range of interests-- in short, a goal that the application must achieve.
Core concerns (core concern): business function modules, such as deposit module, withdrawal module, transfer module, etc.
Crosscutting concerns (crosscutting concern): non-functional, crosscutting modules, such as security management, transaction management, performance monitoring, etc.
Aspect (aspect): one aspect is the modularization of a crosscutting concern, which brings together code that would otherwise be scattered to implement that concern.
Field access: read and write instance variables
Method calls: calls to methods (including constructors)
Exception thrown: a specific exception is thrown.
Pointcut: a group of join points that specify when an enhancement should be invoked. Pointcuts are often described by regular expressions or other wildcard syntax, and some AOP implementation techniques also support the combination of pointcuts.
Many AOP frameworks present enhancements in the form of interceptor-the so-called interceptor is such a
Object: when the connection point is called, it receives a callback message. The basic enhancements are:
Pre-enhancement (BeforeAdvice): call the enhancement first before the connection point is called
Post-enhancement (AfterAdvice): after the connection point is called, enhancement is called. In AspectJ, there are three types of post-enhancement:
AfterReturningAdvice: after the call completes successfully (no exception is thrown).
AfterAdvice: after any call to the join point, regardless of whether the call throws an exception.
Surround enhancements (AroundAdvice): such enhancements have complete control over the execution process. In addition to doing its own work, it is also responsible for actively calling the join point to cause the real operation to take place (proceed)-- usually by calling a particular method.
Introduction: adds a method or field to an existing Java class or interface. This technique can be used to implement multiple inheritance in Java or to attach a new API to an existing object model.
Mixin inheritance: an "blending class" encapsulates a set of functions that can be "blended" into existing classes without using traditional inheritance methods. In the case of AOP, blending is achieved through introduction. In the Java language, multiple inheritance can be simulated by blending.
Weaving: integrates aspects into a complete execution process (or a complete class, where weaving is the introduction).
Initerceptor: many AOP frameworks use it to intercept fields and methods (interception). This is followed by a chain of interceptors (interceptor chain) attached at the connection point (such as method interception), and each interceptor on the chain usually calls the next interceptor.
AOP proxy (AOP proxy): that is, an object reference that is advise-that is, such an object reference on which the AOP enhancement will be performed.
Target object (target object): the object instance at the end of the interceptor chain-the concept exists only in frameworks that use the interception mechanism.
Note: the above concept description is quoted from the concept description of AOP in Chapter 8 of "Expert One-on-One J2EE Development without EJB". More exciting parts can be found in the complete content of this chapter.
The above concepts have been well implemented by the Spring AOP framework, related components:
Advisor component
Advice component
Pointcut component
Advised component
AopProxy component
AopProxyFactory component
Figure 1.
Figure 1 is a panoramic view of the dependencies between enhancements, pointcuts, aspects, and AOP agents.
Enhancements and pointcuts form an aspect, where aspect information and target object information are organized into Advised, and AopProxyFactory generates AopProxy from the information saved in Advised
Object, you can get the enhanced object by calling the AopProxy.getProxy () method.
What we need to focus on here is the different types of enhancers and different types of pointcuts.
The two most important subtypes for different pointcut subtypes: static pointcut and dynamic pointcut
Static pointcuts: choose enhancements based on deployment phase information, such as "intercept all getter methods of a specific class"
Dynamic pointcut: choose enhancements based on runtime information, such as "if the return value of a method is null, include it in a pointcut."
Figure 2.
Figure 2 is a refinement of the implementation of Advisor and Pointcut in figure 1. The relationship between the classes in the figure is a little messy intuitively, but the relationship is quite clear if you take a closer look.
Ending with Advisor is the aspect type, and ending with Pointcut is the pointcut type
The reuse relationship between Advisor and Pointcut can be divided into two types: one is combinatorial reuse, and the other is specific inheritance reuse.
Examples of combinatorial reuse such as the relationship between RegexpMethodPointcutAdvisor and AbstractRegexpMethodPointcut
The relationship between NameMatchMethodPointcutAdvisor and NameMatchMethodPointcut
Specific examples of inheritance and reuse, such as the relationship between StaticMethodMatcherPointcutAdvisor and StaticMethodMatcherPointcut
The relationship between DynamicMethodMatcherPointcutAdvisor and DynamicMethodMatcherPointcut
Figure 3.
Figure 3 is a refinement of the implementation of generating AopProxy objects in figure 1
AopProxyFactory generates AopProxy objects through the information provided by AdvisedSupport. There are two ways to generate AopProxy objects: one is dynamic proxy, the other is bytecode enhancement.
It is important to note that ProxyFactory and ProxyFactoryBean are not necessary parts of the functional implementation, the main purpose is to provide programmers with a convenient API for using agents.
Here is a simple sample:
/ / Target object interface. Public interface Target {public String play (int arg);} / / Target object implementation. Public class TargetImpl implements Target {public String play (int arg) {System.out.println ("play method...."); return "[Target:]" + arg;}} / / pre-enhanced public class MyBeforeAdvice implements MethodBeforeAdvice {public void before (Method method, Object [] args, Object target) throws Throwable {System.out.println (method.getName ()) System.out.println ("before method!");} / / Post enhanced public class MyAfterAdvice implements AfterReturningAdvice {public void afterReturning (Object returnValue, Method method, Object [] args, Object target) throws Throwable {System.out.println (returnValue + ": after method") }} / / the pointcut implements public class MyPointcut implements Pointcut {public ClassFilter getClassFilter () {return new ClassFilter () {public boolean matches (Class arg0) {if (arg0 = = TargetImpl.class) {return true;} return false }};} public MethodMatcher getMethodMatcher () {return new MethodMatcher () {public boolean isRuntime () {return false } public boolean matches (Method arg0, Class arg1) {if ("play" .equals (arg0.getName () {return true;} return false } public boolean matches (Method arg0, Class arg1, Object [] arg2) {System.out.println ("aaaaaa"); if ("play" .equals (arg0.getName () {return true;} return false }};}} public class Main {public static void main (String [] args) {Target target = new TargetImpl (); / / Target object Advice beforeAdvice = new MyBeforeAdvice (); / / enhanced Pointcut pointcut = new MyPointcut (); / / pointcut DefaultPointcutAdvisor dda = new DefaultPointcutAdvisor () / / aspect dda.setAdvice (beforeAdvice); dda.setPointcut (pointcut); AdvisedSupport advisedSupport = new AdvisedSupport (); / / provide basic programming methods, advisedSupport.addAdvisor (dda); advisedSupport.addAdvice (new MyAfterAdvice ()); advisedSupport.addInterface (Target.class); advisedSupport.setTarget (target); AopProxy aopProxy = new DefaultAopProxyFactory (). CreateAopProxy (advisedSupport) Target proxy = (Target) aopProxy.getProxy (); System.out.println (proxy.play); ProxyFactory proxyFactory = new ProxyFactory (); / / provides convenient programming. ProxyFactory.addAdvisor (dda); proxyFactory.addInterface (Target.class); proxyFactory.setTarget (target); Target proxy2 = (Target) proxyFactory.getProxy (); System.out.println (proxy2.play (201)); ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean (); / / provide convenient configuration mode proxyFactoryBean.addAdvisor (dda); proxyFactoryBean.addInterface (Target.class) ProxyFactoryBean.setTarget (target); Target proxy3 = (Target) proxyFactoryBean.getObject (); System.out.println (proxy3.play);}}
Note: some conceptual descriptions have been ignored here for simplicity:
1. For introduction, mixing inheritance is not involved.
two。 The management of notification chain, such as the execution order of multiple notification objects at the same pointcut
3. The above description is the Spring AOP framework itself, and the way to use it is programmatic, which is so low-level that we will not use it in 90% of the cases. the mainstream way of use is the configuration declarative use, and the combination of AOP and IoC can exert the power. ProxyFactoryBean provides a limited declarative use, but strictly speaking, it is still programmatic, because ProxyFactoryBean is a FactoryBean. The goal of a FactoryBean is to programmatically replace complex configurations, and the most important thing is that the API it exposes is too low-level. The abstract attribute of the bean element in the configuration file provides limited help to the length of the configuration file. The automatic generation of DefaultAdvisorAutoProxyCreator well hides the low-level API,DefaultAdvisorAutoProxyCreator as a BeanPostProcessor, which is used to complete the integration of the AOP framework and the IoC container. But this approach still does not solve the problem of dealing with low-level API like XXXAdvisor.
With the introduction of xml elements by spring2.x and the emergence of the AspectJ descriptive style of @ Aspect annotations, the use of the Spring AOP framework reaches a fully declarative standard, and this style also benefits the Spring transaction framework, from TransactionProxyFactoryBean classes to xml elements, and @ Transactional annotations
Original: http://www.iteye.com/topic/1114645
So that we only need to focus on the high-level description, without involving the low-level API.
Attach the class structure diagram of DefaultAdvisorAutoProxyCreator:
Summary:
To fully understand the relevant concepts of AOP, it is necessary to answer the following questions.
one. The background of the concept of AOP, the problems to be solved by AOP, and the concepts involved in AOP
two. What are the problems you need to pay attention to in implementing an AOP framework?
three. Comparison between different AOP Framework implementations
four. Some side effects of AOP are discussed.
After reading the above, have you mastered the method of structural analysis of Spring AOP framework implementation? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.