What is AOP (Aspect Oriented Programming)?

zhaozj2021-02-16  45

What is AOP?

Mountain bridge http://www.jdon.com 2004/01/09

Why is the J2EE container and J2EE application?

We know that J2EE application is only available in the J2EE container to run, then why is it divided into J2EE container and J2EE application? Through the analysis of J2EE container operation mechanisms (see my electronic textbook "EJB practical principle"), we can find that the J2EE container has separated some general features of the general application system, such as transaction mechanism, security mechanism, and object pool or thread. Pool and other performance optimization mechanisms.

These functional mechanisms are almost required for each application, so they can be separated from the specific application system to form a universal framework platform, and the design development of these functional mechanisms has a certain difficulty, while operating stability and rapidity. It is very important to accumulate long debugging and running experience, thereby forming a special J2EE container server product such as Tomcat JBoss, WebSphere, WebLogic, etc.

From the J2EE system, it is divided into two aspects of J2EE containers and J2EE applications, and we have seen a Separation Of Concerns.

Disperse

Separate general demand functions from irrelevant classes; at the same time, many classes can share a behavior, once the behavior changes, there is no need to modify many classes, as long as this behavior can be modified.

AOP is this programming method that realizes dispersion, it will "pay attention" into "aspect".

What is AOP?

AOP is OOP continuation, which is an abbreviation of Aspect oriented Programming, meaning aspect programming.

Example: Suppose there is a shared data must be simultaneously accessed by concurrent data, first, in the data object, called Data Class, and there will be multiple access classes, specifically used for Access this same data object at the same time.

In order to complete the function of the above-mentioned concurrent access to the same resource, it is necessary to introduce the concept of lock Lock, that is, at a moment, when there is an access class to access this data object, this data object must be locked locked Locked, immediately unlocked UNLOCKED, re-access.

Using traditional program habits, we will create an abstract class, all access classes inherit this abstract parent class, as follows:

Abstract class worker {

Abstract void Locked ();

Abstract void accessDataObject ();

Abstract void unlocked ();

}

Disadvantages:

The AccessDataObject () method requires related code such as "lock" status. Java only provides a single inheritance, so the specific access class can only inherit this parent class. If the specific access class also inherits other parent classes, such as another parent class such as Worker, will not be able to implement. Reuse is discounted, the specific access class is only reused in the relevant "lock", which can only be reused in the case where the "lock" is also reused. Carefully study the "lock" of this application, in fact, the following characteristics: "Lock" function is not the primary or primary function of the specific access class, and the main function of the access class is to access the data object, such as reading data or change action. "Lock" behavior is actually independent and distinguishing between the main functions of the specific access class. The "lock" function is actually a longitudinal section of this system involving many classes, many ways. As shown below:

Therefore, a new program structure should be the longitudinal section of the system, such as the "lock" function of this application, this new program structure is aspect (aspect) in this application, "lock" (aspect) should have the following duties :

Provide some essential features to lock or unlock the visited objects. To ensure that all Lock () locks can be called before the operation of modifying the data object, the unlock () unlocked after it is used.

AOP application range

Obviously, AOP is ideal for developing J2EE container servers. At present, JBoss 4.0 is developing using the AOP framework.

The specific functions are as follows:

Authentication permissions

Caching cache

Context passing content

Error Handling error handling

Lazy loading lazy loading

Debugging debugging

Logging, Tracing, Profiling and Monitoring record tracking optimization calibration

Performance Optimization Performance Optimization

Persistence persistence

Resource pooling resource pool

Synchronization synchronization

Transactions transaction

Is AOP necessary?

Of course, the above application examples are also solved without using AOP, such as 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, and AOP may provide a valuable tool. It can be said: Because the AOP structure is used, the source code of JBoss 4.0 is more likely to understand than JBoss 3.x, which is very important for a large complex system.

From another aspect, it seems that all people need to care about AOP, which may be a selection of architectural design. If the J2EE system is selected, the above general aspects of AOP have been implemented by the J2EE container, J2EE application system developer It may be necessary to pay more attention to the industry application aspect.

AOP specific implementation

AOP is a concept that does not set the implementation of the specific language, which overcomes the shortcomings of only single inheritance characteristics (such as Java), and the current AOP has the following items:

AspectJ (TM): Created in Xerox Parc. There are nearly ten years of history, mature

Disadvantages: too complicated; destroying packages; need special Java compiler.

Dynamic AOP: Use JDK dynamic proxy API or bytecode bytecode processing technology.

The specific items based on the dynamic agent API are:

JBoss 4.0 JBoss 4.0 Server

Nanning This is a project named in Nanning, China, is not clear why China is related? Is the Chinese initiated?

Bytecode-based projects are:

Aspectwerkz

Springframework

In other articles in the future, I will continue to analyze the concept of AOP, and learn progress with you.

转载请注明原文地址:https://www.9cbs.com/read-25925.html

New Post(0)