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 attention to separate universal 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. AOP is actually a continuation of GOF design patterns. The design pattern is tired and pursuing is the decoupper between the caller and the caller, and AOP can be said to be an implementation of this goal.
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 cut surface 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 responsibilities:
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.
The AOP application is clear, AOP is ideal for developing J2EE container servers, and current JBoss 4.0 is developing using the AOP framework. Specific functions are as follows: Authentication Permissions Caching content delivery cache Context passing Error handling Error handling Lazy loading lazy loading Debugging debug logging, tracing, profiling and monitoring records to track Picture Performance optimization performance optimization Persistence Persistence Resource pooling resources pool Synchronization Synchronization Services Transactions
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 shortcomings: too complicated; destroying packages; need special Java compiler.
Dynamic AOP: Use JDK dynamic proxy API or bytecode bytecode processing technology. Based on dynamic agent APIs include: JBoss 4.0 JBoss 4.0 Server Nanning This is a project named in Nanning, China, unclear why China is related? Is the Chinese initiated? Bytecode-based items are: AspectWerkz Spring? In other articles in the future, I will continue to analyze the concept of AOP, and learn progress with you.
Reference: http://www.voelter.de/data/articles/aop/aop.htmlaspect-oriented_java_development.pdf