Learn Aspectj with me (1)

zhaozj2021-02-16  41

Learn Aspectj with me (1)

Editor's words

About AspectJ development materials seem to have only English version, but not a lot, this is not a very good developer for interested learning AOP, so I decided to summarize some relevant AOPs Java realizes the use of AspectJ and grammar, so there is an emergence of aspectj, this series is based on the program guide included in Xerox's AspectJ products, and adds me someone. Some understandings and examples, but because I have to prepare IELTS exams, I may not be so on, and I don't have a good time because I have the English level, so if the reader finds some translation techniques, Also, please read the officials, thank you.

Quote

Many software developers are deeply attracted to aspective programming technology (AOP), but it is hard to use this technology. They know the concept of crosscutting concerns, and they have indeed encountered this problem. But how is the process of introducing AOP into the development process? The following problems usually appear:

· Can I use aspects on existing code?

· What kind of benefit can you get after use?

· How to find a few in the program?

· What is the AOP learning curve?

· What is the risk of using this new technology?

This article will focus on these questions in the AspectJ environment. Aspectj is an extension of the Java language. How to get an AspectJ and configure me to see the article aspectJ installation and configuration guidelines in 9CBS.

This article describes some basic concepts for AspectJ and describes these concepts in the form of code snippet (a little bit, these concepts are very strange, not representative, so it is recommended to use English name).

Of course, it is very risky to select new technologies, because usually the new things are particularly fast, and it is not entirely changed. It is very conservative for new technologies for this considering many companies. Fortunately, aspects can be used in different stages of development, and can be implemented without changing the original code, so if it doesn't think it is, it can easily remove its impact. Aspects of the development phase can be used in the program's modulation, unit testing, and performance monitoring; and in terms of product stages, it is fully realized in Java applications; of course, it is also possible to design and develop reusable aspects, very good!

Those who have concerns can fully use only the development phase, which is tracked with the original cumbersome repetition, such as the code execution of the code.

Introduction to AspectJ

This part I will briefly introduce some of the basic concepts in Aspectj. If you are familiar with their readers, you can skip you. And aspectj interest begins with the topic).

In the form of the example of this article, the concept of the use of examples, which helps everyone understand, this paper uses an example of a simple graphic editing system (using an example in the document with aspectj), its UML map is as follows

Figure 1: UML diagram of FigureEditor example

The motivation of Aspectj (that is, AOP) is to find problems that use traditional programming methods that cannot be treated well. Consider an issue to implement security policies in some applications. Safety is a problem that runs through all system modules. Each module needs to apply security mechanism to ensure the security of the entire system. It is clear that the implementation of the security policy here is a cross-cut point, using traditional programming to solve this The problem is very difficult and it is easy to generate an error, which is just when the AOP works.

Traditional object-oriented programming, each unit is a class, and similar to security this issue, they usually cannot focus on a class to handle because they have across multiple classes, which caused the code to not be reused. Maintenance is poor and produced a lot of code redundancy, which is what we don't want to see. The appearance of facing aspects just gives us to the dark, which is treated for these cross-cutting points, just like object-oriented programming. Aspectj, as one of the specific implementation of AOP, JOIN POINT, JOINT, JOINT, is only the name of a Java concept. It adds a little new structure to the Java language: PointCut, notification (Advice), and aspects (aspect). Subscriptions and notifications dynamically affect the program process, the type declaration is a static impact program class level structure, and aspect is a package of all of these new structures.

A connection point is a point specified in the program stream. Clearing points collect specific connection points and values ​​in these points. A notification is the code that is executed when a connection point arrives, these are the dynamic part of AspectJ. In fact, the connection point is like a statement in a program, and the cleaning point is a breakpoint set by a specific statement. It collects information about the program stack at the breakpoint, and the notification is to join before and after this breakpoint. code. There are also many different types of statements in Aspectj, which allows programmers to modify the static structure, name, members of the program, and the relationship between classes. Aspects in AspectJ are module units that cross cut points. Their behavior is very similar to the class in the Java language, but aspect is also packaged, notifications, and type declarations.

Dynamic connection point model

Any key element-oriented key element is a connection point model. Aspectj provides a number of sets of connection points, but this article only describes one of them: Method calls the connection point set. A method calls the connection point capture object. Each runtime method call is a different connection point. Many other connection points may be transported during the method call connection point, including all connection points in the method, and other methods of other methods in the method. We said that these connection points are executed in the dynamic environment of the originally called connection point.

Cut-off point

In AspectJ, the cut point capture a specific connection point collection in the program stream. For example, cutting point

Call (void point.setx (int))

Capturing the connection point called Void Point.setX (int)), that is, the Void Setx method for calling the Point object. Stempoint can be combined with other cut points or (||), with (&&), and non-(!) Operators. For example, Call (Void Point.setx (int)) || Call (Void Point.Sety (int)) captures the connection point of SetX or Sety call. Clearing points can also capture different types of connection points, in other words, they can cross the type. E.g

Call (void figureelement.setxy (int, int)) || call (void point.setx (int))

|| Call (Void Point.Sety (int) || Call (Void Line.Setp1 (Point))

|| Call (Void Line.Setp2 (Point);

Capture any of the two methods calls to any of the connection points. It captures all connection points set when figureElement movements in the examples of this article. Aspectj allows the programmer to name a collected point for use in order to notify us. For example, you can name it for those of the above.

PointCut Move ():

Call (void figureelement.setxy (int, int)) || call (void point.setx (int))

|| Call (Void Point.Sety (int)) || Call (Void Line.Setp1 (Point) || Call (Void Line.Setp2 (Point)); no matter when, programmers can use Move () instead Capture these complex cut points.

The cutouts mentioned earlier are based on display method, which are called name-based. Aspectj also provides another cross-cut, called a property-based cross-cut. They can be signed using wildcard description methods, such as Call (Void Figure.make * (..)) to capture the connection point that calls any way to call the parameter list at the beginning of Make. Call (Public & Figure. * (..)) captures any public method calls in the Figure Object. But wildcards are not unique properties supported by AspectJ, and many other properties are available for programmers in AspectJ. For example, Cflow, it generates an identifier connection point collection based on whether the connection point collection is in a dynamic environment of other connection points. For example, Cflow (Move ()) captures the connection point that occurs in a dynamic environment that is captured by the moving point ().

Notice

Although the cut point is used to capture the collection of connection points, they don't do anything. To truly implement cross-cut behavior, we need to use the notification mechanism. The notification contains the cleaner and the code segment to be performed at each connection point. ASPECTJ has several notifications.

• Before Advice When you reach a connection point but execute before the program process is running. For example, the pre-method is run before the method is actually called, and the parameters just have been analyzed after the method.

Before (): Move () {system.out.println ("The object will move");

· After an advice runs after executing the program process at a particular connection point. For example, after a method call is notified after the method is run, it is just before the control returns the caller. Because the Java program has two forms of exiting connection points, normal and thrown abnormalities. There are three afterwards: after returning, after a notification, an abnormal notice and a clear notice (AFTER), the so-called clear notice is that after normal or abnormal notice is performed. Just like the Finally statement in Java.

After () returning: move () {system.out.println ("The object is just successful");

• In the surrounding notice, after the connection point arrives, the display control program process is executed (no discussion)

Exposure point environment

The cutout point not only captures the connection point, but it can also expose some of the partial execution environments at the connection point. The values ​​exposed in the cleansing point can be declared in the notice. The notification statement has a parameter list (and method) used to describe the name of the environment it uses. For example, after notice

AfTER (figureElement Fe, Int x, int y) Returning: somepointcuts {somecodes}

Used three exposed environments, a FigureEleMent object called FE, two integer variables x, y. The notification body can use these variables like the parameters of the method, such as

AfTER (FigureElement Fe, Int x, int y) Returning: somepointcuts {

System.out.println moves to (" X ", " Y ") ").

}

The notification point releases the value of the notification parameter, three native cut points, THIS, TARGET, and ARGS to release these values ​​/ so the complete code of the above example is

After (FigureElement Fe, Int x, int y) Returning: Call (Void FigureElement.setxy (int, int) && target (fE) && args (x, y) {

System.out.println moves to (" X ", " Y ") ").

}

The target object is figureElement, so FE is the first parameter of the After, the method called two integer parameters so X and Y are the second and third parameters of the AFTER. So notify the print out method setXY call to return the rear object to move to the points x and y. Of course, you can also use naming points to complete the same work, for example

Pointcut Setxy (FigureElement Fe, Int x, int y): Call (void figureelement.setxy (int, int)

&& Target (Fe) && args (x, y);

AfTER (FigureElement Fe, Int x, int y) Returning: Setxy (Fe, X, Y) {

System.out.println moves to (" X ", " Y ") ").

}

Type declaration

Aspectj's type declaration refers to statements that span and their level structure. These may be a change in inheritance relationship between multiple classes. Unlike notifications, it is dynamically operated, and the type declares the static operation when compiling. Consider how to join a new method in the Java language to a certain class, this requires a specific interface, all classes must implement interface declarations within their own, and use Aspectj to use these job utilization. In one aspect. This aspect declares methods and fields and then contact them with the needs of the needs.

Suppose we want to have a Sreen object to observe the changes of the Point object, when Point is an existing class. We can write one aspect, by this aspect statement has an instance field OBSERVERS to save all the reference to the Screen object of the Point object, thereby implementing this feature.

Ask POINTOBSERVING {

Private collection point.observers = new arraylist ();

......

}

The OBServers field is a private field, and only PointObserve can be used. Therefore, to join the method to manage the OBServers aggregation in Aspect.

Ask POINTOBSERVING {

Private collection point.observers = new arraylist ();

Public Static Void AddobServer (Point P, Screen S) {

p.Observers.Add (s);

}

Public Static Void RemoveobServer (Point P, Screen S) {

P.Observers.remove (s);

}

......

}

Then we can define a cut point STATECHANGES to determine what we want to observe and provide an After notification definition when we want to do when observed changes.

Ask POINTOBSERVING {

Private collection point.observers = new arraylist ();

Public Static Void AddobServer (Point P, Screen S) {

p.Observers.Add (s);

}

Public Static Void RemoveobServer (Point P, Screen S) {

P.Observers.remove (s);

}

PointCut StateChanges (POINT P): Target (p) && call (void point.set * (int)); after (POINT P): StateChanges (P) {

Iterator it = p.observers.iterator ();

While (it.hasnext ()) {

UpdateObserver (p, (screen) it.next ()));

}

}

Private Static Void UpdateObserver (Point P, Screen S) {

S.display (p);

}

}

Note whether Sreen or Point is not modified, all new features are implemented in terms of aspect, cool!

aspect

Aspects are packaged all cut points, notifications, and type declarations in the form of cross-cut module units. This is very like a class of Java languages. In fact, aspects can also define their own methods, fields, and initialization methods. As aspects like classes can also be declared using the AbstractRace keyword as an abstract aspect, and can be inherited by the sub-aspect. The design in AspectJ actually uses a single sample mode, default, it cannot use the New configuration, but a method can be used, such as method aspectof (), can be used. Therefore, non-static member fields can be used in terms of notices.

E.g

Aspect traacing {

Outputstream trace = system.out;

AfTER (): move () {trace.println ("Successful Mobile");}

}

to sum up

This article is the first article of this series, the purpose is to let readers have a basic understanding of AOP's Java implementation, and understand several important concept connection points, cleansing points in Aspectj, and notice that there is also a type of statement. The next article will discuss the application of the development phase and the product phase.

More information

1. Aspectj Installation and Configuration Guide

2. AspectJ implementation design mode (1) - observer mode

Reference

1. The aspectjtm programming guide http://www.eclipse.org/aspectj/

statement

This article retains copyright by STARCHU1981, and if you need to pay, please write the author and source.

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

New Post(0)