1. Order
Aspect oriented Programming (AOP) is a relatively hot topic recently.
ASPECTJ is the implementation of the AOP Java language and has gained extensive attention from Java programmers.
For specific information about ASPECTJ and AOP, look in the following links:
http://www.eclipse.org/aspectj/
http://www.parc.com/research/csl/projects/aspectj/
http://aosd.net/
The first contact with ASPECTJ readers to see this material (or grammar manual), will feel aspectj somewhat mysterious.
They want to know how ASPECTJ did it? How does the ASPECTJ work? Does ASPECTJ require a special operating environment?
This paper starts with explaining the design idea and operating principle of ASPECTJ, and answers the above questions.
This article explains the main content, according to the concept of the importance of the following arrangement:
The ASPECTJ is a code-generation tool (Generator).
The ASPECTJ syntax is the syntax used to define code generation rules. If you have used Java Compiler Compiler (JavaCC), you will find that the concept of code generation rules is strikingly similar.
ASPECTJ has its own syntax compiler tool, the result of compiling is Java class file, when running, classpath need to contain aspectj of a jar file (runtime Lib).
Comparison of AspectJ and Xdoclet. Comparison of ASPECTJ and EJB descriptor.
2. Aspect oriented Programming (AOP)
This section briefly introduces the concept of AOP and explains why we need AOP.
AOP is a complement to object oriented programming (OOP).
OOP can solve the problem of object's data and encapsulation well, but it can't solve the problem of separation of aspect ("aspect") well. The following are examples of specific.
For example, we have a bank class. Bank has two methods, deposit (save) and withdraw (take money).
The classes and methods are defined as follows (Code 2.1 Bank.java):
Class Bank
{
Public float deposit (accountinfo account, float money)
{
Increase the amount of the account and return the current amount in the account
}
Public float Withdraw (accountinfo account, float money)
{
Reduce the amount of money in account accounts and return the amount of money taken out
}
};
These two methods involve the user's account funds and other important information, must be very careful, so after writing the above business logic, the project leader also put forward new requirements-to the bank class each important method plus security authentication characteristics.
Therefore, we have to add the security authentication code in the above two methods separately.
The classes and methods are defined as follows (Code 2.2 Bank.java):
Class Bank
{
Public float deposit (accountinfo account, float money)
{
Verify that the account is a legitimate user
Increase the amount of the account and return the current amount in the account
}
Public float Withdraw (accountinfo account, float money)
{
Verify that the account is a legitimate user
Reduce the amount of money in account accounts and return the amount of money taken out
}
};
Both methods need to operate the database, in order to maintain the integrity of the data, the project leader has put forward new requirements-to the bank class of each operational database method plus transaction control.
As a result, we have to add the transaction control code to the above two methods separately.
The classes and methods are defined as follows (Code 2.3 Bank.java):
Class Bank
{
Public float deposit (accountinfo account, float money)
{
Verify that the account is a legitimate user
Begin Transaction
Increase the amount of the account and return the current amount in the account
End Transaction
}
Public float Withdraw (accountinfo account, float money)
{
Verify that the account is a legitimate user
Begin Transaction
Reduce the amount of money in account accounts and return the amount of money taken out
End Transaction
}
};
We see that these repetitive code, unrelated to business logic, is pervasive throughout the program. The actual projects involved in the classes and functions, far more than two. How to solve this problem?
The authentication, transaction, etc. mentioned in this example is the aspect that AOP cares about.
AOP is a solution to this problem. The purpose of AOP is--separation of aspects (or separation of concerns).
The following sections explain how the ASPECTJ tool solves the problem of separation of aspects.
3. Aspectj
In this section we take a look at how ASPECTJ implements the separation of aspects in the above example.
With ASPECTJ, we do not have to make any changes to the original code to provide different aspect (aspects) for the code-for example, authentication, transactions, etc.
We only need to provide two different aspect--certified aspect and transaction aspect.
Code 4.1 Authaspect.java
Aspect Authaspect
{
Pointcut bankmethods (): Execution (* bank.deposit (...)) | | Execution (* Bank withdraw (...));
Object around (): Bankmethods ()
{
Verify that the account is a legitimate user
return proceed ();
}
};
Code 4.2 Transactionaspect.java
Aspect Transactionaspect
{
Pointcut bankmethods (): Execution (* bank.deposit (...)) | | Execution (* Bank withdraw (...));
Object around (): Bankmethods ()
{
Begin Transaction
Object result = proceed ();
End Transaction
return result;
}
};
If you do not understand this code for the time being, there is no relationship, but later on, these aspect definitions simply define some code generation rules.
We compile the bank file with the AspectJ compiler and the file containing the aspect, and the result is a bank class with security authentication and transaction processing. The compiled bank class calls the AspectJ runtime lib, so if you want to run the bank class, you need to set ASPECTJ runtime lib in your classpath.
Let's take a look at what the AspectJ compiler did for us.
First, AspectJ all the file names from the file list, and then reads the files for analysis.
AspectJ found that some files contain aspect definitions, in this case Authaspect and Transactionaspect, which are code generation rules.
AspectJ Modify Add your source code based on these aspect code generation rules. In this example, the add bank file is modified.
AspectJ reads the definition of Authaspect and discovers a pointcut--bankmethods (); This pointcut definition is execution (* bank.deposit (...)) | | Execution (* Bank withdraw (...)), which represents all the execution points for the deposit and withdraw methods of the Bank class.
AspectJ continues to read the definition of Authaspect and discovers a around (), which is called aspectj in advice, I don't understand why it's called, but it doesn't matter, we just have to know what it is. Advice allows you to add additional code before or after a call to a method of a class. The "//Verify account is a legitimate user" section of around () in Code 4.1 shows the code to be added. Where does this piece of code add to? Around () followed by a pointcut--bankmethods (). According to this POINTCUT,ASPECTJ will add this code to the Bank.deposit and Bank.withdraw two methods before the execution. The effect is as shown in code 2.2.
AspectJ read the definition of Transactionaspect, as in step (4), and found a pointcut--bankmethods ().
AspectJ continues to read the definition of Authaspect and discovers a around (). This time aspectj the "Begin Transaction" and "End Transaction" Two pieces of code added to the Bank.deposit and bank. Withdraw two methods before and after the execution. The effect is as shown in code 2.3.
How do I verify this? You can go to http://www.eclipse.org/aspectj/to download the installation ASPECTJ, compile the sample inside, compile the results to decompile, you can see the ASPETJ automatically generated code.
As we can see, ASPECTJ is an automatic code generation tool. You write a common code, such as authentication code, transaction code, and then define a set of code generation rules according to ASPECTJ syntax (aspect definition), ASPECTJ will help you automatically distribute this generic code into the corresponding code, simple and fast, scrutinise.
Coincidentally, a well-known compiler generation tool,--java Compiler Compiler (JavaCC), also uses a very similar code generation mechanism. JAVACC allows you to include your own Java code in the syntax definition rules file to handle the various syntactic elements that are being read into.
ASPECTJ makes your code leaner and more structured. AspectJ the benefits, I will not say much, online a lot of wonderful articles to explore the various uses of ASPECTJ.
4. Summarize
The emergence of open source projects, breaking the many barriers in the field of software technology, to promote the rapid development of software technology.
At the same time, a number of new concepts are emerging, dazzling, confused. In fact, a lot of things are new, we understand the application of these technologies, to seize the essence, to superstition, to break any artificial mystery.
The authority of the internet age is not to say it, but to do it.
In addition, around a number of promising new technologies, there will always be a large number of "Quick Start Handbook", some is simply the translation of the technical help document, and the difficulty of the place is not translated, we all understand the place is very detailed translation, detailed to the point of no need. This is due to the market demand for the occasional time of the text, a large number of technical articles in the field.
Java AOP-ASPECTJ