AspectJ Study Notes [02]

Source: Internet
Author: User

Connection Point Model

In the system, not all connection points can be used, but in the system, the connection point selected by the cut point is called the exposed connection point. Aspectj only allows you to use a subset of all system connection points. For example, you cannot expose a loop connection point, because then you can easily change a for loop to a while loop, the functions of these two loops are the same, but the usage habits are different. The notification declaring for the connection point can only be used on the connection point. The connection points exposed in aspectj include: Call and execution of methods, Object Instantiation, field access, and exception handling.

All connection points are associated with a context. For example, the context of the connection point of a method call includes the call object, target object, and parameter list of the method. The cut points can capture the context of these connection points and can be used in the notification body.

Exposed connection point type

Method connection point (Method join points)

Aspectj exposes two types of connection points for each method: Execution and call connection points. The execution connection point is the method body, and the call connection point is another part of the program, which is generally located at the position of the Code that calls the method.

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

Public class account {

...

Public void debit (float amount)

Throws insufficientbalanceexception {

If (_ balance <amount ){

Throw new insufficientbalanceexception (<

"Total balance not sufficient"); <debit Method

} Else {<execution connection point

_ Balance-= amount; <

} <

} <

}

The call connection point of a method is located at the location where the method is called, for example:

Account account = ...;

Account. debit (100); <-call connection point of the debit Method

Note that the parameter Part Of The method does not belong to the connected part.

Construct a connection point (Constructor join points)

The exposed connection points in the constructor are the same as most method connection points, Besides the execution logic of creating an object . The execution connection point for constructing the connection point is the constructor itself, and the call to the connection point is located in the part of the object creation logic. A typical method is to influence object creation. For example, when creating an object, you can use the constructor to bypass creation (without creating a new object ), use the previously created object.

Field access connection point (Field access join points)

Field access connection points capture read and write access to an instance or class member of a class. Note that only the connection points of data members of the Rule class are exposed, that is, access to local variables in the method is not exposed.

A typical use of these connection points in a notification is to ensure 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 accountnumber ){

_ Accountnumber = accountnumber; <-write access connection point

}

...

}

Exception Handling execution connection point (Exception handler execution join points)

An exception handling execution connection point depicts an exception-type processing block.

Try {

Account. debit (amount );

} Catch (insufficientbalanceexception ex ){

PostMessage (ex); <exception

OverdraftManager. applyOverdraftProtection (account, <Process

Amount); <connection point

}

Class initialization connection point (Class initialization join points)

The class initialization connection point depicts the loading of a class, including the initialization of the static part. This connection point is used for class-level cross-cutting.

Public class Account {

...

Static {

Try {<

System. loadLibrary ("accounting"); <class initialization

} Catch (UnsatisfiedLinkError error) {<connection point

... Deal with the error <

} <

}

...

}

Object initialization connection point (Object initialization join points)

Object initialization connection points capture the initialization of an object, starting from the returned constructor of the object's parent object until the end position of the constructor that calls the object for the first time.

Public class SavingsAccount extends Account {

...

Public SavingsAccount (int accountNumber, boolean isOverdraft ){

Super (accountNumber );

_ IsOverdraft = isOverdraft; <-object initialization connection point

}

Public savingsaccount (INT accountnumber ){

This (accountnumber, false); <-object initialization connection point

}

...

}

Object pre-initialization connection point (Object pre-initialization join points)

Public class savingsaccount extends account {

...

Public savingsaccount (INT accountnumber ){

Super (accountnumber,

Accountmanager. internalid (accountnumber) <-object pre-initialization connection point

);

_ Type = accountconstants. savings;

}

...

}

Notification execution connection point (Advice execution join points)

The notification execution connection point includes all the notification execution bodies in the system.

Public aspect MannersAspect {

Before (): deliverMessage (){

System. out. print ("Hello! "); <Notification execution connection point

}

}

Public aspect LoggingAspect {

After (): loggedOperations (){

... <

_ Logger. log (...); <notification execution connection point

... <

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.