Java framework-AOP framework

Source: Internet
Author: User
AOP is a continuation of OOP and short for aspect oriented programming, which means Aspect-Oriented Programming. AOP is actually a continuation of the gof design model. The design model tirelessly pursues decoupling between callers and callers. AOP can be said to be an implementation of this goal. AOP is a programming method that implements decentralized attention. It encapsulates "Focus" in "aspect. Generally, an object is "bonded" by a line of code. Create this object. Create the object. Set attributes for the object (whose value is this object. Some user data is also dotted. Stir everything together. This is something that modern programmers often do when designing and coding.
When multiple such classes are combined to form components with certain functions, many such components are connected in this way, the problem arises: to implement different methods, it takes a lot of time to write the same code. These lines of code usually have the following operations: log the activity of this method into a file for debugging, run the security check, start a transaction, open a database connection, remember to capture C ++ exceptions or Win32 structured exceptions to convert to com exceptions, and verify the parameters. Also, remember the settings when the destruction method starts after the method is executed. There are also many transaction mechanisms, security mechanisms, object pools, thread pools, and other performance optimization mechanisms.
This type of repetitive code often occurs because developers are trained to design the system based on the terms in the software project requirements. If the banking system is designed, account and customer categories are essential, they collect their unique details in one place, however, each of these methods also requires operations such as logs, security checks, and transaction management. The difference is that operations such as logs are system aspects unrelated to specific applications.
These functional mechanisms are almost required by every application system. Therefore, they can be separated from a specific application system to form a general framework platform, the Design and Development of these functional mechanisms are difficult, and the stability and speed of running at the same time are very important. It must be developed after a long time of debugging and operation experience.

In this way, we already have a way of thinking about decentralization (separation of concerns ).
The common requirement function is separated from no related classes. At the same time, many classes share an action. Once the behavior changes, you do not have to modify many classes. You only need to modify this behavior. This is the separation of concerns ).
AOP is a programming method that implements decentralized attention. It encapsulates "Focus" in "aspect.
Aspect-oriented programming (AOP) is a programming paradigm invented by Xerox PARC in 1990s, it allows developers to better separate tasks that shouldn't be entangled with each other (such as mathematical operations and Exception Handling. The AOP method has many advantages. First, because the operation is more concise, the performance is improved. Second, it allows programmers to spend less time rewriting the same code. In short, AOP can provide better encapsulation for different processes and improve future interoperability.
What makes software engineers want to become hardware engineers? Since the invention of the function, programmers have spent a lot of time (and most of their boss's money) trying to design such a system: they are just a combination of models, made up of parts created by others, it is arranged in a unique shape and then covered with some pleasing colors. Functions, templates, classes, components, and so on are all attempts by software engineers to create "software integrated circuits" (simulating the electronic devices of hardware designers.
I blame this on Lego ). The cool click of the two toys (Component) is addictive, and many programmers will invent a new mechanism of encapsulation and reuse. The latest developments in this area are called Aspect-oriented programming (AOP ). The core of AOP is to arrange components (one stacked on the other), so that you can obtain the level of reuse that cannot be obtained by other component-based development methods. This arrangement is performed in the call stack between the client and the object. The result is that a specific environment is created for the object. This environment is exactly what AOP programmers are pursuing.

What is AOP?
AOP is a continuation of OOP and short for aspect oriented programming, which means Aspect-Oriented Programming. AOP is actually a continuation of the gof design model. The design model tirelessly pursues decoupling between callers and callers. AOP can be said to be an implementation of this goal.
For example, assume that in an application system, a shared data must be accessed concurrently. First, encapsulate the data in a data object called data class, there will be multiple Metadata classes dedicated for accessing the same data object at the same time.
To achieve the preceding concurrent access to the same resource, the lock concept must be introduced. That is to say, when a metadata class accesses this data object at a certain time point, the data object must be locked, unlock unlocked immediately after use, and then provide access to other unlock classes.
Using traditional programming habits, we will create an abstract class. All the class classes inherit this abstract parent class, as shown below:
Abstract class worker {
Abstract void locked ();
Abstract void accessdataobject ();
Abstract void unlocked ();
}
Disadvantages:
The accessdataobject () method requires Code such as the "Lock" status.
Java only provides single inheritance. Therefore, a specific worker class can only inherit this parent class. If a worker class also inherits other parent classes, for example, another worker parent class, it cannot be conveniently implemented. Reuse is compromised. Because the specific classification class also contains Code related to the "Lock" status, it can only be reused when there is a "Lock". The Reuse scope is very narrow.
The "Lock" of this application has the following features:
The "Lock" function is not the primary or primary function of a specific category. The primary function of the category is to access data objects, such as reading or changing data.
The "Lock" behavior can be separated from the main functions of a specific category.
The "Lock" function is actually a vertical aspect of the system and involves many classes and methods.
Therefore, a new program structure should focus on the longitudinal aspect of the system, such as the "Lock" function of the application. The new program structure is aspect (aspect ).
In this application, aspect has the following responsibilities:
Provides essential functions to lock or unlock the accessed objects. To ensure that all the operations on the data object can be called to lock (), after it is used, call unlock () to unlock.

What is aspect?
When considering the relationship between objects and other objects, we usually think of inheriting this term. For example, define an abstract class-Dog class. Inheritance is usually used to extend functions when you identify similar classes but each class has its own unique behavior. For example, if a poodle is identified, a poodle is a dog, that is, a poodle inherits the dog. So far it seems good, but what if I define another unique behavior that will mark it as obedient dog? Of course, not all dogs are very tame, so the dog class cannot contain obedience behavior. In addition, if you want to create an obedient dog class inherited from dog, which position of the poodle hierarchy is appropriate? Poodle is a dog, but poodle is not necessarily an obedient; so does poodle inherit from dog or obedient dog? None of them. We can regard tame as an aspect and apply it to any type of tame dog. We oppose forcing this behavior into the dog hierarchy in an inappropriate way.
In terms of software, Aspect-oriented programming can be applied to change the class or object behavior independently of any inherited hierarchy. Then, these aspects are applied at runtime or during compilation. Give an example of AOP and describe it. First, define four key AOP terms, which is important because I will use them repeatedly:
& #8226; joinpoint-clearly defined identifiable points in the code.
& #8226; pointcut-a method for specifying a joint point by configuring or coding.
& #8226; advice-indicates a method for performing a cross-cutting action
& #8226; Mixin-introduces a new behavior by mixing an instance of a class into the instance of the target class.
To better understand these terms, you can regard the joint point as a point defined in the Program Stream. A good example of a joint point is: when the Code calls a method, the point that is called is considered as a joint point.
The cut point is used to specify or define the joint points to be intercepted in the Program Stream. A cut point also contains a notification that occurs when the joint point is reached. Therefore, if a cut point is defined on a specific method called, The AOP framework intercepts the cut point when the method or joint point is called, and a cut point notification is also executed.
There are several types of notifications, but the most common case is to regard them as another method to be called. When calling a method with a cut point, the notification to be executed will be another method to be called. The notification or method to be called can be an intercepted method in the object, or a method in another mixed object.

Is AOP necessary?
Of course, the above application examples have also been solved without using AOP. For example, JBoss 3. xxx also provides the above application functions, but does not use AOP.
However, using AOP allows us to understand software systems from a higher abstract concept. AOP may provide a valuable tool. This can be said: Because the AOP structure is used, the source code of JBoss 4.0 is much easier to understand than JBoss 3.x, which is very important for a large complex system.
From another aspect, it seems that not all people need to care about AOP. It may be an architectural design option. If you choose a J2EE system, the above general aspects of AOP have been implemented by J2EE containers. J2EE application system developers may need to pay more attention to the aspect of industrial applications.

Specific implementation of AOP
AOP is a concept that does not set the implementation of a specific language. It can overcome the disadvantages of a single inheritance feature language (such as Java ), the Application of AOP to most large systems or key production systems is not yet completely mature, but with the improvement of language support, the application of AOP will be easier. In addition, increasing support is also a new software development example, for example, using a software factory for Aspect-Oriented Programming. Currently, there are several available AOP frameworks, each of which has its own methods, positive attributes, and negative attributes. Currently, the specific implementation of AOP includes the following items:
& #8226; aspectj (TM): created in Xerox PARC. has a history of nearly ten years and is mature.
Disadvantages: Too complicated; destroys encapsulation; Special Java compiler is required.
& #8226; dynamic AOP: Uses JDK's dynamic proxy API or bytecode processing technology.
Specific projects based on dynamic proxy APIs include:
JBoss 4.0 JBoss 4.0 Server
Jac (Java aspect components)-is an application server. It is a Java 2 platform, an enterprise development environment (J2EE) for Java Development, and a web-based distributed application, provides another option for open resources (released under the GNU General Public License ). Jac includes the Unified Model Language (UML) IDE, which is a modular application of commercial logic and automatically generates and compiles pure commercial logic Java classes. These classes are executed within the JAC container and can be performed from a set of technical and/or commercial cross-cutting relationships (crosscutting concerns) such: seamless benefit from data persistence, authentication, configuration file management, access permission detection, demonstration, and load balancing. Based on the Aspect-Oriented Programming Technology (AOP), Jac separates these relationships from the core business logic of the application.
Nanning is a project named after Nanning, China. Why is it related to China? Initiated by the Chinese?
& #8226; bytecode-based projects include:
Aspectwerkz-Java-based simple, dynamic, lightweight, and powerful AOP framework. It is both powerful and simple, and helps you easily integrate AOP into new or existing projects.
Spring
Spring. Net-A. Net version of the popular Java Spring framework. AOP will be implemented in the next version.
& #8226; dynamicaspects-allows you to use the Aspect-Oriented Programming compiled in Java. It uses the "Instrumentation" and "agent" median in Sun JDK 1.5 ", aspects can install and use the relationship between various software modules during runtime.
& #8226; dynaop framework-use a runtime-based programming mechanism to insert AOP code into an object, rather than returning an object with feature code. AOP will be a new field of object-oriented design (OO.
& #8226; Caesar-is a new Java-compatible AOP language. All Java programs can use Caesar.
& #8226; prose-is a weaving tool that allows you to insert or extract aspects at runtime ). Prose aspects is the rule that Java objects can be sent to or received from computers on the network. Signatures can be used to ensure their integrity. Once an aspect is inserted into the JVM, any event will affect the execution result of the corresponding aspect advice. If an aspect is revoked from JVM, the aspect code will be discarded and the corresponding Interception will not happen again. Prose aspects is the rule that Java objects can be sent to or received from computers on the network. Signatures can be used to ensure their integrity. Once an aspect is inserted into the JVM, any event will affect the execution result of the corresponding aspect advice. If an aspect is revoked from JVM, the aspect code will be discarded and the corresponding Interception will not happen again.
& #8226; encase-encase the application can be added to the object separately during the runtime.
& #8226; aspect #-a cli-based AOP joint compatibility framework that provides built-in languages for declaration and configuration.
& #8226; application of the rail-Rail framework in the JIT class of virtual machines.
& #8226; EOS-an aspect-oriented extension for C.

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.