Main concepts of AOP (favorites)

Source: Internet
Author: User
ArticleDirectory
    • Main concepts of AOP
Main concepts of AOP

==== Focus (Concern) ==== The focus is the problem we want to investigate or solve. Such as order processing, user verification, and user log records are all of the concerns. . Focus Core concern ( Core concerns) Is the core function of the system, that is, the real business logic. For example, in an e-commerce system, order processing, customer management, inventory and logistics management are all the core concerns of the system. Another concern is Cross-concern (Crosscutting concerns) They solve the same problem in each module and span multiple modules. Such as user verification, log management, transaction processing, and data cache are all cross-cutting concerns. In AOP The main focus of the programming method is to raise and abstract the focus. . We can Consider a complex system as being implemented by a combination of multiple concerns. A typical system may have several considerations, such as core business logic, performance, data storage, logs, authorization, security, thread, and error check, there are also concerns in the development process, such as ease of maintenance and scalability.


====Section (Aspect) ==== Aspect is a modular focus , This concern may cross multiple objects and modules. Transaction Management is a good example of cross-concern. It is an abstract concept, from the software perspective Is in the ApplicationProgramA field or aspect in different modules . In spring 2.0 AOP, the aspect can be used XML Schema-based Style Or use @ Aspect Annotation ( @ Aspectj The following example defines an aspect (red part) based on the XML schema style ): <AOP: aspect id = "aspectdemo" ref = "aspectbean"> < AOP: pointcut ID = "Mypointcut" Expression = "Execution (* package1.foo. Handle *(..)" /> < AOP: before Pointcut-ref = "Mypointcut" Method = "Dolog" /> </AOP: aspect> This definition means that the dolog method of aspectbean will be executed before the method starting with handle of package1.foo class is executed. ====Connection Point (Join point) ==== The connection point is a specific point in the program execution process, such as when a method is called or when an exception is handled. This can be a method, an attribute, constructor, a class static initialization block, or even a statement. For spring 2.0 AOP, the connection point can only be a method. Remember this ~! Each method can be viewed as a connection point. Only the jointpoint incorporated into a certain cutpoint can be considered as advice. . By declaring Org. aspectj. Lang. joinpoint You can obtain the connection point information for the main part of the notification (advice. Joinpoint For the relationship between them, see the description of the cutpoint below. ====Entry Point (Pointcuts) ==== An entry point refers to one or more connection points, which can be understood as a set of power points connected. Advice connects and intervene in your jointpoint through pointcut. For example, in the previous example < AOP: pointcut ID = "Mypointcut" Expression = "Execution (* package1.foo. Handle *(..)" /> This defines Pointcut , Pointcut Indicates that Package1.foo All Handle Method starting" Hypothesis Package1.foo Class is similar: Public class Foo { Public handleupload (){..} Public handlereadfile (){..} ..... } So Handleupload Is Jointpoint , Handlereadfile It is also Joint , Then Id = "mypointcut" Of Pointcut The two Jointpoint . ====Notification (advice) ==== Advice Defined Actual Logic in the Section (that is, implementation) For example, the actual log writingCode. In other words Advice It refers to the program code to be executed at the defined entry point. . There are several types of notifications: Before advice) : Run and use it before executing the method matching the entry point @ Before Annotation to declare Notification after return (after returning advice) : Execute the command when the method of the entry point match returns. Use @ Afterreturning Annotation to declare After throw notification (after throwing advice) : Run when an exception is thrown during execution of the method that matches the start point. Use @ Afterthrowing Annotation to declare Post notification (after (finally) Advice) : Whether the matching method of the entry point ends normally or an exception ends, the notification (after (finally) Advice) will run after it ends. Use @ After Annotation to declare. This notification must be properly handled in two cases: Normal Return and abnormal return. It is usually used to release resources. Surround notification (around advice) : The surround notification runs both before and after the method that matches the start point. In addition, it can determine when the method is executed, how it is executed, or even whether it is executed. In In the surround notification, in addition to freely adding required cross-cutting functions, you must also take the initiative to call the connection point (through proceed) to execute the program to activate the connection point. . Try to use the simplest notification that meets your needs. (For example, if the pre-notification can also be applied, do not use the surround notification) The @ around annotation is used to declare the surround notification. And the connection The first parameter of the corresponding method must be of the proceedingjoinpoint type. . In the notification body (that is, within the specific method of notification ), Call proceedingjoinpoint's proceed () method to execute the Connection Point Method . ====Introduction (Introduction) ==== Introduction refers to adding methods or field attributes to an existing class, The introduction also allows existing Java classes to implement new interfaces without changing the existing class code. ( And a corresponding implementation ). Introduction is used to change the static structure of a class than advice can dynamically change the functions or processes of a program. . For example, you can use an introduction to implement the ismodified interface of bean to simplify the cache mechanism. ====Target object)==== The object notified by one or more aspect (advise. Someone also calls it an advised object. Since Spring AOP is implemented through runtime, this object is always a proxied object. ====AOP Proxy (AOP proxy) ==== AOP Objects Created by the framework to implement aspect contract (including notification method execution and other functions ). In spring, the AOP proxy can be JDK dynamic proxy or cglib proxy. Note: Spring 2.0 The newly introduced Schema-based style and @ aspectj annotation style statement. for users who use these styles, the creation of the AOP agent isTransparentOf . Spring uses j2se by default. Dynamic proxy (Dynamic proxies) As the proxy of AOP. In this way, any interface can be proxy. Spring also supports cglib proxy. cglib proxy is necessary when proxy classes are required instead of proxy interfaces. If a business object does not implement an interface, cglib is used by default. As Interface-Oriented Programming Business Objects usually implement one or more interfaces. ====Weaving ==== Connect the aspect (aspect) to another application type or object and create an object to be notified (advised). Such an action is called weaving. These can be completed at compilation (for example, using the aspectj compiler), class loading and runtime. Spring Like other pure Java AOP frameworks . In fact, the weaving methods are as follows: 3 Type: 1 ,Run-Time woven-In Java Use Java Provides a proxy for weaving. Depending on the Generation Method of the agent, running-time weaving can be further divided J2se Dynamic proxy and dynamic bytecode generation. Because J2se Dynamic proxy can only act as a proxy interface. Therefore, you must use some dynamic bytecode generators to implement dynamic proxy for classes.Majority AOP The implementation adopts this running mode, including Spring . 2 ,Woven by the Class Loader-It refers to the use of custom class loaders in virtual machines JVM It is woven when the bytecode is loaded, for example Aspectwerkz ( Merged Aspecj) And JBoss This method is used. . 3 ,Compiled by the compiler-A special compiler is used to compile the entire application, including the aspect module, which is most powerful during compilation. Compiled by the compiler AOP The implementation is generally based on Language extension, that is, through the standard Java Language for some simple extensions, add some for processing AOP Module keywords, define a set of language specifications, use this set of language specifications to develop the aspect module, use your own compiler to generate Java Bytecode. Aspectj This is mainly used for weaving. .

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.