1. Introduction
The goal of software development is to establish a model of some elements or information flow in the world, and to realize the engineering of software system needs to decompose the system into modules that can be created and managed. So the object-oriented programming technology with modularization of system is presented. Modular object-oriented programming extreme polar increases readability, reusability and extensibility of software systems. The focus of the object method is to select the object as the primary unit of the module and to associate the object with all the behavior of the system. Object becomes the main element of the problem domain and the computational process. But object-oriented technology does not fundamentally solve the reusability of software systems. When creating a software system, there are many crosscutting concerns in the real world, such as security checks, logging, performance monitoring, exception handling, etc., and their implementation code and other business logic code are mixed together and scattered in different parts of the software (the code that handles these operations is added directly to each module) , this undoubtedly destroys the "single duty" principle of OOP, and the reusability of the module is greatly reduced, which makes the maintainability and reusability of the software system greatly limited. At this time traditional oop design often take the strategy is to join the corresponding agent (proxy) layer to complete the functional requirements of the system, but such processing obviously makes the whole system to add a level of division, the complexity of the increase, thus giving people too thick feeling. This has resulted in aspect-oriented programming (AOP) technology. This programming pattern extracts the crosscutting concerns code scattered throughout the software system, and is modular and organised to further improve the maintainability, reusability, and scalability of the software.
2.AOP Introduction
Aop:aspect oriented programming is programming for facets.
Plane-oriented programming (also called aspect-oriented): Aspect oriented Programming (AOP) is a hotspot in software development at present. AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development.
AOP is the continuation of OOP, the abbreviation for (Aspect oriented programming), which means programming for facets (aspect).
The main functions are: Logging, performance statistics, security control, transaction processing, exception handling and so on.
The main intent is to divide the code of logging, performance statistics, security control, transaction processing, exception handling, and so forth from the business logic code, and by separating these behaviors, we want to be able to separate them into non-instructional methods of business logic, and then change these behaviors without affecting the code of the business logic.
A technology that can dynamically and uniformly add functionality to a program without modifying source code is possible through precompilation and run-time dynamic proxies. AOP is actually a continuation of the GOF design pattern, and the design pattern pursues the decoupling between the caller and the callee, and AOP can be said to be an implementation of this goal.
If the application is thought of as a stereoscopic structure, the blade of OOP is a longitudinal cut-in system, which divides the system into many modules (e.g. user module, article module, etc.), while the blade of AOP is the horizontal cut-in system, which extracts the parts that each module may want to repeat (such as: Permission check, logging, etc.). Thus, AOP is an effective complement to OOP.
Note: AOP is not a technique, it is actually a programming idea. Any technology that conforms to the idea of AOP can be seen as an implementation of AOP.
3. What is Aspect programming
When considering the relationship between objects and objects, we often think of inheriting the term. For example, define an abstract class-dog class. Inheritance is often used to extend functionality when identifying similar classes but each class has its own unique behavior. For example, if you identify Poodle, you can say that a Poodle is a dog, that Poodle inherits from the dog. It all seems good, but what if you define another unique behavior that is later identified as Obedient Dog? Of course, not all Dogs are tame, so the Dog class cannot contain obedience behavior. Also, if you want to create a Obedient dog that inherits from dog, where does Poodle fit in this hierarchy? Poodle is a dog, but Poodle is not necessarily obedient, so is Poodle inherited from dog or obedient dog? No, we can look at taming as an aspect and apply it to any kind of tame dog, and we object to forcing the behavior into the dog hierarchy in an inappropriate way.
4. Differences from OOP object-oriented programming
AOP and oop are literally very similar, but they are two design ideas for different fields. OOP (Object-oriented programming) encapsulates the entity and its attributes and behaviors of the business process in an abstract way for clearer and more efficient logical unit partitioning.
AOP is the extraction of facets in the process of business processing, which is faced with a step or stage in the process of processing, in order to obtain the isolation effect of low coupling between parts of the logic process. These two kinds of design thought have the essential difference in the goal.
The above statement may be too theoretical, for example, to encapsulate a business entity like "employee", which is naturally a oop/ood task, and we can create an "employee" class for it and encapsulate the attributes and behaviors associated with "employee". However, it is impossible to encapsulate "employee" with the idea of AOP design.
Similarly, the segmentation of the action fragment for permission checking is the target area of AOP. It is a bit nondescript to encapsulate an action by Ood/oop.
In other words, Ood/oop is for the field of nouns, and AOP is for the verb field.
Basic concepts of 5.AOP
In object-oriented programming, the concepts of class, object, encapsulation, inheritance, polymorphism and so on are the main terms that describe object-oriented thinking. Similarly, there are some basic concepts in aspect-oriented programming:
Junction Point (Jointpoint): A specific point during the execution of a junction program. A typical junction point is the invocation of a method, the execution of the procedure itself, the initialization of the class, initialization of the object, and so on. Junction points are one of the core concepts of AOP and are used to define where the program is to add new logic through AOP.
Pointcut (Pointcut): A pointcut is a set of junction points that defines when a notification is to be executed. By defining pointcuts, we can precisely control what components receive notifications in the program. As we mentioned above, a typical junction point is a method call, and a typical pointcut is a collection of method calls to a class. Usually we have a complex pointcut to control when notifications are executed.
Notification (Advice): code that runs at a particular junction point is called a notification. There are many kinds of notifications, such as
The pre-notification (before advice) that was executed before the junction point and the post-notification (after advice) that was executed after the junction point.
Aspect (Aspect): A combination of notifications and pointcuts is called an aspect, so the facet defines the logic that should be included in a program and when it should be executed.
Weaving (Weaving): Weaving is the process of actually adding aspects to the program code. For static AOP scenarios, weaving is done at compile time, usually by adding a step in the compilation process. Similarly, the dynamic AOP scheme is dynamically woven into the program when it is running.
Target: If an object's execution is modified by an AOP, it is called a target object. The target object is also often referred to as the notified object.
Introduction (Introduction): By introducing, you can add a new method or property to an object to change its structure so that if the object's class does not implement an interface, you can modify it so that it becomes an implementation of the interface.
Static and dynamic: The difference between static AOP and dynamic AOP is mainly in what time it is woven and how it is woven into. Most of the earliest implementations of AOP are static. In static AOP, weaving is a step in the compilation process. In Java terminology, static AOP accomplishes the weaving process by directly manipulating bytecode, including modifying code and extending classes. Obviously, this approach produces good performance because the final result is plain Java bytecode, which no longer requires special skill at runtime to determine when notifications should be executed. The disadvantage of this approach is that if you want to modify the aspect, you must recompile the entire program, even if you are adding a new junction point. AspectJ is a typical example of static AOP. Unlike static AOP, the weaving of dynamic AOP is done dynamically at runtime. How the weaving is done, each implementation is different. Spring AOP takes the form of an agent, and then the agent executes the notification at the appropriate time. One of the weaknesses of dynamic AOP is that it is generally less performance than static AOP. The main advantage of dynamic AOP is that it is possible to modify all aspects of the program at any time without recompiling the target.
5.1 crosscutting Technology
"Crosscutting" is the proper term for AOP. It is a relatively simple design and programming technique that contains powerful power, especially when it comes to building loosely coupled, scalable enterprise systems. Crosscutting techniques enable AOP to move through a given set of responsibilities (such as logging and performance optimization) in a specific programming model.
What happens to software development without using crosscutting techniques? In traditional programs, because the implementation of crosscutting behavior is decentralized, it is difficult for developers to implement or change these behaviors logically. For example, the code used for logging is intertwined with code that is primarily used for other duties. Depending on the complexity of the problem being solved and the scope of the difference, the resulting confusion can be large and small. Changing the logging policy for an application can involve hundreds of edits-even if it works, which is a daunting task.
In AOP, we refer to the behavior of these common logic, which is entangled with the core logic of other modules, as "crosscutting concerns (crosscutting Concern)" because it spans the typical line of duty in a given programming model.
5.2 Crosscutting concerns
A point of concern (concern) is a specific purpose, a region of interest to us, a logical act we need. From a technical point of view, a typical software system contains some core concerns and system-level concerns. For example, the core focus of a credit card processing system is loan/deposit processing, while system-level concerns are log, transaction integrity, authorization, security, and performance issues, and many of the concerns-crosscutting concerns (crosscutting concerns)-occur in multiple modules. With existing programming methods, crosscutting concerns traverse multiple modules, resulting in a system that is difficult to design, understand, implement, and evolve. AOP provides a better separation of system concerns than the above approach, providing a modular crosscutting focus.
For example, a complex system, which is implemented by a number of focus points, such as business logic, performance, data storage, logging and scheduling information, authorization, security, threading, error checking, etc., as well as the focus of the development process, such as easy to understand, easy to maintain, easy to trace, easy to expand and so on.
1. a group of concerns that are implemented by different modules form a system that the module is implemented as a group of concerns .
By identifying the requirements and implementation of the system, we can divide these concerns in the module into core concerns and crosscutting concerns. For core concerns, the modules that implement these concerns are usually independent of each other, and they complete the business logic required by the system, which is related to the specific business requirements. For the logs, security, persistence and other concerns, they are the business logic module common needs, these logic is distributed in the core focus of the various places. In AOP, such modules are referred to as crosscutting concerns. The key to the application of AOP is to realize the recognition of the focus point.
2. Identify the point of concern
If the entire module is likened to a cylinder, then the focus recognition process can be described by the Prism law, through the prism beam (refers to the demand), irradiation to the cylinder everywhere, to obtain different color of the beam, and finally identify the different concerns.
1). Attention Point Identification: Prism law,:
In the identified concerns, business logic is a core concern, and it calls to cross-cutting concerns such as security,logging,persistence.
Public class businesslogic { publicvoid someoperation () { // Verify Security ; securtity Focus;// record log before execution; logging focus; dosomething (); // Save the data after the logical operation; persistence focus; // execution end logging; logging focus; } }
3. Weaving crosscutting concerns into core focus points
The purpose of AOP is to separate crosscutting concerns such as logging from the Businesslogic class. Using AOP technology, the relevant crosscutting concerns can be encapsulated to form a separate "aspect". This guarantees the reuse of crosscutting concerns. Because the Businesslogic class no longer contains the logical code for crosscutting concerns, to achieve the purpose of invoking crosscutting concerns, you can take advantage of crosscutting techniques to intercept messages for related methods in the Businesslogic class, such as the Someoperation () method, and then add these " Aspect "is woven into this method. Weave cross-cutting concerns into core focus points,
Through the use of AOP technology, the entire system has changed the way the design. At the beginning of analyzing the system requirements, we use the idea of AOP to isolate the core concerns and cross-cutting concern points. After implementing common logic for crosscutting concerns such as logging, transaction management, and permission control, developers can focus on core concerns and devote their energies to solving business logic. At the same time, these packaged crosscutting concerns provide the ability to maximize the reuse of various parts of the business logic, without the need for special coding by developers or the impact of specific business functions by modifying the functionality of crosscutting concerns.
6.AOP Practice
6.1 Java Practice
In WEB program development, we know that because of the stateless nature of HTTP protocol, we usually need to keep the user's state information in Session. In some scenarios, users need to be logged in to continue.
Traditional Implementation Methods :
For this reason, before we perform each business operation, the traditional implementation method adds the following logic:
protected void doPost (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { = request.getsession (); if (Session.getattribute ("user") = =null) { request. Getrequestdispatcher ("login.jsp" ). Forward (REQ,RESP); } Dospecialbussinesslogic (); }
The logic implemented in this way requires the programmer to do this in the place where the login check should be implemented. This inevitably causes a lot of duplication and confusion in the code. Here the login check logic is a non-primary logic, and our main logic is dospecialbussinesslogic (), the main logical and non-main logic of chaos is a major limitation of traditional programming methods.
Implemented with AOP technology:
The advent of AOP provides a good solution to the above problems. The following is the implementation of the login check logic completed with ASPECTJ:
public aspect LOGINCHECKAOP {Pointcut Logincheck (httpservletrequest req, HttpServletResponse resp): (Execution ( void *.. *action.dopost (HttpServletRequest, HttpServletResponse)) && args (REQ,RESP); public before (HttpServletRequest req, HttpServletResponse resp): Logincheck (REQ,RESP) {HttpSession session = request. GetSession (); if (Session.getattribute ("user") == Null "login.jsp" ). Forward (REQ,RESP); } } }
We define an aspect named LOGINCHECKAOP, and the ASPECTJ compiler automatically inserts the code of the login check logic into the desired place by name matching. There are several advantages to using an AOP method for login checking than manually inserting a check code where needed.
• Only need to place all the function codes that need to be checked in one (LOGINCHECKAOP) location.
• It is easy to insert and delete check codes. It is easy to re-implement different aspects of the inspection without having to modify the other code.
• Log-in checks wherever needed, even if new methods or classes are added. This can eliminate human error. Also know that all login check codes have been removed, and that nothing will be overlooked when we remove aspects from the build configuration.
• There is a reusable aspect that can be applied and upgraded.
7. Conclusion
Aspect-oriented programming is a new technology that excites software developers, and it is used to look for novel modularity features in software systems. Aspect-oriented programming is a complement to object-oriented programming technology, and there is no competition between them, in fact, they complement each other and complement each other in software development. Aspect-oriented programming as a new programming technology, it has a very bright application prospects.
This paper summarizes the online information, including Baidu Encyclopedia AOP Baidu Library: Aspect-oriented programming technology research and practice
AOP aspect-oriented (tangent) programming