Spring Architecture unveils-AOP

Source: Internet
Author: User

1. Overview of AOP

2. Spring AOP principle

3. Spring AOP Architecture Parsing

I. Overview of AOP

Java programmers often write code by using the new object class to describe business features, and then through the object of inheritance, composition, extension and other means to achieve business requirements, which is typical of the face object programming is OOP, generally speaking, OOP is a vertical programming mode from top to bottom.

In the actual programming, often encountered in some methods will be used to the same logic, such as notation execution log, the code in OOP can not be completely decoupled from the business code, so that the method code has mixed logic, does not conform to the single principle of responsibility for programming. If this logic code can be decoupled, pass the two logic code area, and then in the run time together perfect, it is equivalent to the operation of the horizontal building blocks, that is, the aspect of horizontal programming is AOP.

Two, Spring AOP principle

The main technology to realize AOP is dynamic agent, the principle of dynamic agent is not discussed here, mainly to see the implementation of Spring AOP principle download. We know that the dynamic agent technology commonly used is JDK and cglib, where the JDK dynamic agent can only broker the interface level, while Cglib can also proxy class level, which combines both in spring to make it more flexible.

Before you go into spring AOP principles, take a look at some of the following important concepts:

A, connection point (Joinpoint): A specific location of the program execution, such as before class initialization, after the class is initialized, before the method executes, after the method executes, after the exception is thrown, and so on.

B, Pointcut (PointCut): Specify where the program executes, each class object has more than one joinpoint, so on which joinpoint to execute? This needs to be specified with pointcut, usually a pointcut can correspond to multiple joinpoint.

c, Enhancement (Advice): The enhancement is to weave into the target class connection point of a program code, many places call it notice, I feel called reinforcement more can express its meaning. There are beforeadvice,afteradvice and so on in Spring. Where the AOP Alliance defines the standard interface advice,spring AOP is also based on its extension.

D, Facets (Aspect): Facets are the combination of tangent points and enhancements, and Spring AOP is the implementation framework for facets that implement the enhanced logic defined in facets to the point of connection specified in the slice.

E, proxy: When a class is woven into enhanced code, it produces a proxy class that is actually executed at run time.

The following is a diagram of the principles of spring AOP. Download

650) this.width=650; "height=" 525 "width=" src= "http://dl2.iteye.com/upload/attachment/0119/8813/" 7d29b6c8-5276-3603-9255-4390a0e568ea.jpg "title=" Click to view the original size picture "class=" Magplus "style=" border:0px; "/>

Spring will scan the relevant configuration when initializing the container, including XML, annotation, etc., after discovering that the bean labeled aspect will find the target class based on the tangent point definition in the tangent plane, and weave the enhanced code into it and generate the proxy object through the proxy generator and then be used by the user.

Third, Spring AOP schema parsing download

The following diagram illustrates the main components of spring AOP and its component structure, and first looks at the component diagram of Spring AOP

650) this.width=650; "height=" 525 "width=" src= "http://dl2.iteye.com/upload/attachment/0119/8811/" 6a012da4-3634-3cba-8612-fd95c195c9c7.jpg "title=" Click to view the original size picture "class=" Magplus "style=" border:0px; "/>

Spring's AOP is mainly composed of four components, namely the tangent component, the slice component, the enhancement component, the proxy component. Download

Based on these four components, take a look at the specific class diagram.

Tangent Component Class Diagram:

650) this.width=650; "height=" 493 "width=" src= "http://dl2.iteye.com/upload/attachment/0119/8823/" 6995f6f5-687d-37fd-9497-bfdd4e491402.jpg "title=" Click to view the original size picture "class=" Magplus "style=" border:0px; "/>

The Pointcut interface defines only two methods, one is Getclassfilter () to get the filter, whether the pointcut can be used on the target class, and the other is Getmethodmatcher () to get the method match. Spring provides a static and dynamic method to match the download, in addition to other methods of matching device, here is not listed, static method matching only according to the method signature matching, do not care about the parameters of the runtime, only one time, and the dynamic method of the match for each method to execute the parameters may not be the same, So each invocation method needs to be judged, the performance impact is large.

Enhanced Component Class Diagram:

650) this.width=650, "height=" width= "src=" http://dl2.iteye.com/upload/attachment/0119/8815/ 32f1b786-0347-38c9-a676-c6919ea9f7aa.jpg "title=" Click to view the original size picture "class=" Magplus "style=" border:0px; "/>

AOP Federation defines the standard of AOP, one of which is the advice enhanced interface, which is based on which the major AOP frameworks extend, with spring extending afteradvice,beforeadvice on its basis, Dynamicintroductionadvice,inteceptor is also an interface defined by the AOP Federation for surround enhancement, i.e., weaving enhanced logic downloads before and after method execution

Proxy Component Class diagram

650) this.width=650; "height=" 544 "width=" 525 "src=" http://dl2.iteye.com/upload/attachment/0119/8821/ 75aaa516-ca23-30ca-b313-1ff1defa65ec.jpg "style=" border:0px; "/>

In spring, when the container is initialized, scanning to the slice class takes the proxy object through Proxyfactory's GetProxy () and then joins the bean memory for use. It is clear that spring supports the two dynamic proxy methods of JDK and cglib.

Slice Component Class diagram

650) this.width=650; "height=" 380 "width=" 583 "src=" http://dl2.iteye.com/upload/attachment/0119/8817/ 90e52898-0d95-39a3-83d3-c1bd4654dabe.jpg "style=" border:0px; "/>

AOP General Architecture Class diagram

650) this.width=650; "height=" 269 "width=" 501 "src=" http://dl2.iteye.com/upload/attachment/0119/8819/ 5a58a1de-dcf4-3457-8761-302a89f74e4c.jpg "style=" border:0px; "/>

The structure of the spring AOP framework and the composition of its components are clearly known through the final general schema class diagram.


Spring Architecture unveils-AOP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.