Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is AOP?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what is AOP, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.

What is AOP?

Why distinguish between J2EE containers and J2EE applications?

We know that J2EE applications can only run when deployed in a J2EE container, so why is it divided into J2EE containers and J2EE applications? Through the analysis of the running mechanism of J2EE container (see my electronic textbook "practical principles of EJB"), we can find that J2EE container actually separates some general functions of general application systems, such as transaction mechanism, security mechanism and performance optimization mechanism such as object pool or thread pool.

These functional mechanisms are needed by almost every application system, so they can be separated from specific application systems to form a general framework platform. moreover, the design and development of these functional mechanisms is difficult, and the stability and rapidity of running at the same time are very important, which must be accumulated through long-time debugging and running experience. therefore, a special J2EE container server product is formed. Such as Tomcat JBoss, Websphere, WebLogic and so on.

From the two aspects that J2EE system is divided into J2EE container and J2EE application system, we have seen a way of distracting attention (separation of concerns).

Distract attention

The common requirements function is separated from the related classes; at the same time, it enables many classes to share a behavior, and once the behavior changes, there is no need to modify many classes, just modify the behavior.

AOP is such a programming method to achieve distraction, encapsulating "concerns" in "aspects".

What is AOP?

AOP is a continuation of OOP, an abbreviation for Aspect Oriented Programming, which means aspect-oriented programming. AOP is actually the continuation of the GoF design pattern, which assiduously pursues the decoupling between the caller and the callee. AOP can be said to be a realization of this goal.

For example: suppose that in an application system, there is a shared data that must be accessed concurrently at the same time. First, the data is encapsulated in a data object, called Data Class. At the same time, there will be multiple access classes dedicated to accessing the same data object at the same time.

In order to accomplish the function of accessing the same resource concurrently, it is necessary to introduce the concept of lock Lock, that is, at some point, when an access class accesses the data object, the data object must lock the Locked, and then unlock the unLocked immediately after use, and then make it accessible to other access classes.

Using traditional programming habits, we will create an abstract class, and 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 a "lock" state.

Java only provides 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, it will not be easy to implement.

Reuse is discounted, and specific access classes can only be reused in related "lock" situations because they also contain related code such as "lock" status, and the scope of reuse is very narrow.

A careful study of the "lock" of this application shows that it actually has the following characteristics:

The "lock" function is not the primary or main function of a specific access class, the main function of the access class is to access data objects, such as reading data or changing actions.

The "lock" behavior is actually independent and distinct from the main functions of the specific access class.

The "lock" function is actually a vertical aspect of the system, involving many classes and methods.

Therefore, a new program structure should focus on the vertical aspect of the system, such as the "lock" function of this application, and the new program structure is aspect (aspect).

In this application, the "aspect" side should have the following responsibilities:

Provide some necessary functions to lock or unlock the accessed object. To ensure that all operations before modifying the data object can call lock () to lock, after it has been used, call unlock () to unlock.

Application range of AOP

Obviously, AOP is very suitable for developing J2EE container servers, and JBoss 4.0 is currently developed using the AOP framework.

The specific functions are as follows:

Authentication permission

Caching caching

Context passing content delivery

Error handling error handling

Lazy loading lazy load

Debugging debugging

Optimal Calibration of logging, tracing, profiling and monitoring record tracking

Performance optimization performance optimization

Persistence persistence

Resource pooling resource pool

Synchronization synchronization

Transactions transaction

Is AOP necessary?

Of course, the above application example has 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, and AOP may provide a valuable tool. It can be said that because of the AOP structure, the source code of JBoss 4.0 is much easier to understand than JBoss 3.x, which is very important for a large and complex system.

On the other hand, it seems that not everyone needs to care about AOP, it may be a choice of architecture design, if you choose J2EE system, the above general aspects of AOP concern have been realized by J2EE container, J2EE application system developers may need to pay more attention to industry application aspect aspect.

The concrete realization of AOP

AOP is a concept and does not specify a specific language implementation. It can overcome the shortcomings of those languages with only single inheritance features (such as Java). Currently, AOP has the following specific implementation projects:

AspectJ (TM): created in Xerox PARC. It has a history of nearly ten years and is mature

Disadvantages: too complex; break encapsulation; need a special Java compiler.

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

What is AOP above? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report