1, development environment
l Eclipse 3.0.1
l aspectwerkz2.0 rc2 (http://aspectwerkz.codehaus.org/releases.html)
l aspectwerkz Eclipse Plugin 2.0.4 (http://aspectwerkz.codehaus.org/downloads/eclipse)
2. Enable AspectWerkz for the project
l Create a normal Java project: awhello
l Right-click Engineen name, select Enable AspectWerkz AOP, enable aspectWerkz
l You can view the tracking information generated by the plugin in the Error log view.
3, add dependence JAR
Current plugins do not automatically add dependent AspectWerkz JAR files to the project, need to be manually added, these JAR files include (found in the lib directory of AspectWerkz2.0 RC2):
l askectwerkz-2.0.rc2.jar
l askectwerkz-core-2.0.rc2.jar
l aspectwerkz-jdk14-2.0.rc2.jar (JDK1.4) or aspectwerkz-jdk5-2.0.rc2.jar (JDK1.5)
l Concurrent-1.3.1.jar
l Dom4j-1.4.jar
l jarjar-0.3.jar
l jrexx-1.1.1.jar
l piccolo-1.03.jar
l qdox-1.4.jar
l Trove-1.0.2.jar
4. Create a test program
Package foo;
Public class helloworld {
Public static void main (string args []) {
HelloWorld World = New HelloWorld ();
World.greet ();
}
Public void greet () {
System.out.println ("Hello World!");
}
}
5, write aspect
Package foo;
Import org.codehaus.AaspectWerkz.joinPoint.joinPoint;
Public class myaspect {
Public void beforegreeting (joinpoint joinpoint) {
System.out.println ("Before Greeting ...");
}
Public void aftergreeting (joinpoint joinpoint) {
System.out.println ("After Greeting ...");
}
}
l Myaspect class contains code used to weave to the HelloWorld class
l Note: The Aspect method must contain JoinPoint parameters
6, AOP weaving (weaving)
l Use meta-inf / aop.xml to define aspect (usually in the project / src directory)
Expression = "execution (* foo.helloworld.greet (..))" /> Bind-to = "greetMethod" /> Bind-to = "greetMethod" /> askPECT> package> system> aspectwerkz> l l l Ø This method is just used to bind the advice specified behind Ø Expression attribute: Must be a valid expression in accordance with the Join Point Select mode language, and to ensure that the class name of the full path is included in the mode l Ø Name Property: Specifies the corresponding method name in the aspect class Ø TYPE attribute: Specify the Advice type, can be Before, After or Around Ø Bind-to attribute: Specify the bound PointCut l This example is to insert myaspect.beforegreeting () method before the HelloWorld.greet () method is called; after the HelloWorld.greet () method returns the Myaspect.Aftergreeting () method l When saving the file, trigger Eclipse Build, the AOP woven of AOP is also triggered based on valid meta-inf / aop.xml files or definition in aspect, compiled ANNOTATION (later) 7, AOP cross-view support l AOP woven, you will mark the right pointing mark in the Advice application. l Click this tag to display the Quick fix prompt, including the Advice applying the Join Point and its type. l Click on concrete Advice, it will be positioned to the source code of the advice. l For the current plugin, modify the information of the Aspect class does not update the information at the mark. l Implement updates by modifying the target file or manually executing Clean Build 8, run the program l First add dependence Tools.jar to the project build path (java_home / lib / Tools.jar) l Select Run> Run ... l Select AspectWerkz Application in the list, click the New button l After the method of formal application is followed. l The results of this example are: Before greeting ... Hello World! After Greeting ... 9, Annotations Support l Annotations provides a method of adding metadata in the Aspect class, rather than specified in separate files (AOP.XML). l This plugin contains the AspectWerkz Java 1.4 Annotation compiler, l When compiles the Aspect source code, Annotations will also be compiled. l You can use Javadoc style in the Aspect class to define Annotations (JDK 1.4), below is an ASPECT class using Annotations: Package foo; Import org.codehaus.AaspectWerkz.joinPoint.StaticJoinPoint; Public class myaspectwithannotation { / ** * @Before Execution (* foo.helloworld.greet (..)) * / Public void beforegreeting (staticjoinpoint joinpoint) { System.out.println ("Before Greeting With Annotation ..."); } / ** * @AFTER EXECUTION (* foo.helloworld.greet (..)) * / Public void aftergreeting (staticjoinpoint joinpoint) { System.out.println ("After Greeting with Annotation ..."); } } l For AspectWerkz 2.x versions, you can use StaticJoinPoint to replace JoinPoint to optimize performance. l Using Annotations means not need to describe the details of Aspect in AOP.XML, but still need to specify the Aspect class: Expression = "execution (* foo.helloworld.greet (..))" /> Bind-to = "greetMethod" /> Bind-to = "greetMethod" /> askPECT> package> system> aspectwerkz> l The result of re-run is as follows: Before Greeting with Annotation ... Before greeting ... Hello World! After Greeting ... After Greeting with annotation ...