ASPECTJ is a plane-oriented programming framework that extends the Java language. ASPECTJ defines the AOP syntax so it has a dedicated compiler to generate class files that conform to the Java Byte encoding specification.
ASPECTJ currently supports the following three ways of weaving
Compile-time Weaving: aspect classes (AOP facets) and target classes (AOP classes) are compiled with AJC.
Post-compile-time weaving: The target class may have been made into a jar package, and the jar can be woven again with the AJC command
Load-time weaving load-time weaving (LTW): Bytecode modification or substitution when the JVM loads classes
Reverse engineering can be used in the latter two: after the compile-time weaving and loading when weaving.
1. Installing ASPECTJ
: http://www.eclipse.org/aspectj/downloads.php
Select the 1.9 version of the download, after the download is completed is a jar package
Installation commands
Java-jar Aspectj-1.9.1.jar
Follow the prompts to set up the installation directory for the Java home directory and ASPECTJ, which uses the default C:\aspectj1.9
After the installation is complete, set the following environment variables
Set Aspectj_home
Aspectj_home = C:\aspectj1.9
Add/Modify Classpath, add Aspectjrt.jar
CLASSPATH =.; %java_home%\lib;%java_home%\lib\tools.jar;%java_home%\lib\dt.jar;%aspectj_home%\lib\aspectjrt.jar
New value added for PATH variable
%aspectj_home%\bin
After the environment variable setting is complete, the validation command
Ajc-version
The following results show that the environment variable is configured successfully
2. Take a look at two simple examples
First Project HelloWorld
Java file Helloworld.java
Helloworld.javapublic class HelloWorld {public void SayHello () {System.out.println ("Hello, world!");} public static void Main (string[] argv) {HelloWorld hw = new HelloWorld (); Hw.sayhello ();}}
ASPECTJ file Myaspect.aj
Myaspect.ajpublic aspect Myaspect {pointcut say (): Call (void Helloworld.sayhello ()); before (): Say () { System.out.println ("Before Say hello ...."); After (): Say () {System.out.println ("after say hello .....");}}
Load-time weaving (LTW) load-time weaving command
Compile
Ajc-outjar Myjar.jar helloworld.javaajc-outjar myaspect.jar-outxml myaspect.aj-classpath "myjar.jar;%CLASSPATH%"
Run
Aj5-classpath "myaspect.jar;myjar.jar;%classpath%" HelloWorld
Run results
You can see that there are two lines of text on the HelloWorld string
Second item, output parameter and return value
Java file Main.java
Main.javapackage Com.vvvtimes;public class Main {public int add (int x, int y) {return x + y;} public int Add (int x, int y, int z) {return x + y + z;} public static void Main (string[] args) {main m = new main (); System.out.println (M.add (1, 2)); System.out.println (M.add (1, 2, 3));}}
ASPECTJ file Tracing.aj
Tracing.ajpublic aspect tracing {private pointcut mainmethod (): execution (Public static void main (String[ ]); before (): mainmethod () {system.out.println ("> " + thisjoinpoint); After (): mainmethod () {system.out.println ("< " + thisjoinpoint); Private pointcut addmethod (): execution (Public int add (..)); Before (): addmethod () {system.out.println ("> " + thisjoinpoint); Object[] args = thisjoinpoint.getargs ();for (int i = 0; i < args.length; i++) {system.out.println ("args[" + i + "]: " + Thisjoinpoint.getargs () [I].tostring ());}} After (): addmethod () {system.out.println ("< " + thisjoinpoint);} After () returning (Object o) :addmethod () {System.out.println ("return value: " + o.tostring ());}}
Compile
Ajc-outjar Myjar.jar com/vvvtimes/main.javaajc-outjar tracing.jar-outxml tracing.aj-classpath "Myjar.jar;%CLASSPATH %"
Run
Aj5-classpath "tracing.jar;myjar.jar;%classpath%" Com.vvvtimes.Main
Run results
As you can see, the parameters and return values of the Add method are printed.
By the way, the difference between the three weaving periods is compared from the command line angle as follows:
Compile-time Weaving compile-time weaving
Ajc-outjar Mytarget.jar Helloworld.java Myaspect.aj
Run
Aj5-classpath "mytarget.jar;%classpath%" HelloWorld
Post-compile Weaving after the compile-time weaving
Ajc-outjar Myjar.jar helloworld.javaajc-inpath Myjar.jar Myaspect.aj-outjar Mytarget.jar
Run
Aj5-classpath "mytarget.jar;%classpath%" HelloWorld
Load-time Weaving (LTW) weaving at load time
Compile
Ajc-outjar Myjar.jar helloworld.javaajc-outjar myaspect.jar-outxml myaspect.aj-classpath "myjar.jar;%CLASSPATH%"
Run
Aj5-classpath "myaspect.jar;myjar.jar;%classpath%" HelloWorld
Initial knowledge AspectJ of Java Reverse Foundation