Aspectj learning notes [02]

xiaoxiao2021-03-06  39

Connection point model

In the system is not all connection points can be used, and the system can be cleared to the exposed connection point. AspectJ only allows you to use a subset of all connections in the system, for example, you can't expose a loop connection point, because you can easily change a for loop into a while loop, and the function of these two loops is the same It is just the use habits. Moreover, the notice declared for the connection point can only be used on the connection point. The connection point that can be exposed in Aspectj includes: the method of calling and execution, the object's instantiation, field access, and abnormal processing.

All connection points have a context associated with it. For example, a method called connection point includes a matrix list of call objects, target objects, and methods of the method. The cutpoint can capture these connection points context and can be used in the notification body.

Exposure point type

Method connection point (Method Join Points)

AspectJ exposes two types of connection points for each method: perform and call the connection point. The execution connection is the method body itself, and the call connection point is another portion of the program, typically located in the position of the code to call the method.

The execution of the method includes all the code in the method, such as:

Public class account {

...

Public void debit (Float Amount)

Throws insufficientBalanceException {

IF (_Balance

Throw new insufficientbalanceException (<

"Total Balance Not Sufficient";

} else {

_balance - = amount; <

}

}

}

The call connection point of the method is located at the location called by this method, such as:

Account Account = ...

Account.debit (100); <- DEBIT method call connection point

Note that the parameter portion of the method is not part of the connection.

Constructor Join Points

The exposure connection point in the constructor is the same, the connection point is the same,

In addition to it contains execution logic to create an object

. The execution connection point for constructing the connection point is the constructor itself, and the call connection point is located in the part of the logic of the object. A typical way of use is to affect the creation of the object, for example, when you create an object, you can use the constructive connection point to bypass the creation (not creating new objects), and use the object you have created before.

Field access connection point (Field Access Join Points)

Field Access Connections Capture Access Actions for instances or class members of a class. Note that the connection point of only access to the data of the data will be exposed, that is, access to local variables in the method is not exposed.

Typical usage of using these connection points in the notification is to confirm that the object is correctly initialized before using the object.

Public class account {

INT _ACCOUNTNUMBER;

...

Public string toString () {

Return "Account:"

_AccountNumber <- Read Access Connection Point

...

}

...

}

Public class account {int _accountnumber;

Public Account (int accessNumber) {

_accountnumber = accountnumber; <- write access connection point

}

...

}

Exception Handler Execution Join Points

An exception handler execution connection depicts an exception type processing block.

Try {

Account.debit (Amount);

} catch (insufficientbalanceexception ex) {

Postmessage (ex);

OverdraftManager.ApplyOverDraftProtection (Account,

Amount);

}

Class initialization connection point (Class Initialization Join Points)

Class initialization connection points depicts a class load, including initialization of static parts. This connection point is used for horizontal cutting levels.

Public class account {

...

STATIC {

Try {<

System.loadLibrary ("Accounting");

} catch (unsatisfiedlinker error) {

... DEAL with the error <

}

}

...

}

Object initialization connection point (Object Initialization Join Points)

The object initialization connection point captures an object's initialization, returns from the constructor of the parent object of the object until the end position of the object's constructor is called.

Public class savingsaccount extends account {

...

Public Savingsaccount (int accessNumber, boolean isoverdraft) {

Super (Accountnumber);

_ISOVERDRAFT = ISOVERDRAFT; <- Object initialization connection point

}

Public savingsaccount (int accessnumber) {

This (AccountNumber, False); <- Object Initialization Connection Point

}

...

}

Object pre-initialization join point (Object Pre-Initialization Join Points)

Public class savingsaccount extends account {

...

Public savingsaccount (int accessnumber) {

Super (Accountnumber,

AccountManager.internalId (AccountNumber) <- Object Pre-initialization connection point

);

_Type = AccountConstants.savings;

}

...

}

Notification Execute Connection (Advice Execution Join Points)

The notification execution connection point includes an actuator all notified in the system.

Public aspect mannersaspect {

Before (): DELIVERMESSAGE () {

System.out.print ("Hello!");

}

}

Public aspect loggingaspect {

AfTER (): loggedopertyS () {

... <

_Logger.log (...);

... <

}

}

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

New Post(0)