JBoss AOP works by instrumenting the classes you want to run. This means that modifications to the bytecode are made in order to add extra information to the classes to hook into the AOP library. JBoss AOP allows for two types of instrumentation
This chapter describes the steps you need to take to precompile your classes with the aop precompiler.
JBoss AOP comes with an ant task that you can use for precompiling your classes with the aop precompiler. An example build.xml file is the basis for the explanation. (It is quite similar to the one used in the previous chapter.)
<?xml version="1.0" encoding="UTF-8"?> <project default="compile" name="JBoss/AOP"> <target name="prepare">
Define the source directory, and the directory to compile classes to. If you're not fussy, they can point to the same directory.
<property name="src.dir" value="PATH TO YOUR SOURCE DIR"> <property name="classes.dir" value="PATH TO YOUR DIR FOR COMPILED CLASSES">
Include the jars AOP depends on.
<path id="javassist.classpath"> <pathelement path="../../../javassist.jar"/> </path> <path id="trove.classpath"> <pathelement path="../../../trove.jar"/> </path> <path id="concurrent.classpath"> <pathelement path="../../../concurrent.jar"/> </path> <path refid="jboss.common.core.classpath"/> <path refid="jboss.common.logging.spi.classpath"/> <path refid="jboss.common.logging.log4j.classpath"/> <pathelement path="../../../jboss-common.jar"/> </path> <path id="lib.classpath"> <path refid="javassist.classpath"/> <path refid="trove.classpath"/> <path refid="jboss.aop.classpath"/> <path refid="jboss.common.core.classpath"/> <path refid="jboss.common.logging.spi.classpath"/> <path refid="jboss.common.logging.log4j.classpath"/> <path refid="concurrent.classpath"/> </path>
This snippet shows what to do if you are using JDK 5.0 annotations:
<!-- JDK version 1.5 --> <path id="jboss.aop.classpath"> <pathelement path="../../../jboss-aop-jdk50.jar"/> </path> <!-- JDK version 1.5 - END -->
Now we set up the full classpath of all the needed libraries:
<path id="classpath"> <path refid="lib.classpath"/> <path refid="jboss.aop.classpath"/> </path id="classpath">
As an alternative, we can use the single jar provided with JBoss AOP. This jar bundles all the libraries used by JBoss AOP in a single unit. To use this jar, just define:
<!-- JDK version 1.5 --> <path id="jboss.aop.classpath"> <pathelement path="../../../jboss-aop-jdk50.jar"/> </path> <!-- JDK version 1.5 - END --> <path id="classpath"> <path refid="jboss.aop.classpath"/> </path id="classpath">
Define the org.jboss.aop.ant.AopC ant aop precompiler task:
<taskdef name="aopc" classname="org.jboss.aop.ant.AopC" classpathref="jboss.aop.classpath"/> </target>
<target name="compile" depends="prepare">
Compile the files (from the source directory to the compiled classes directory:
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="on" deprecation="on" optimize="off" includes="**"> <classpath refid="classpath"/> </javac>
Now use the ant aop precompiler task, it reads the files from the
<aopc compilerclasspathref="classpath" verbose="true"> <classpath path="${classes.dir}"/> <src path="${classes.dir}"/> <include name="**/*.class"/> <aoppath path="jboss-aop.xml"/> <aopclasspath path="aspects.jar"/> </aopc> </target> </project>
The org.jboss.aop.ant.AopC ant task takes several parameters.
<aoppath> <pathelement path="jboss-aop.xml"/> <pathelement path="xmldir"/> </aoppath>
<aopclasspath> <pathelement path="aspects.jar"/> <pathelement path="foo.jar"/> </aopclasspath>
To run the aop precompiler from the command line you need all the aop jars on your classpath, and the class files you are instrumenting must have everything they would need to run in the java classpath, including themselves, or the precompiler will not be able to run.
The jboss.aop.path optional system property points to XML files that contain your pointcut, advice bindings, and metadata definitions that the precompiler will use to instrument the .class files. The property can have one or files it points to delimited by the operating systems specific classpath delimiter (';' on windows, ':' on unix). Files or Directories can be specified. If it is a directory, JBoss AOP will take all aop.xml files from that directory.
The jboss.aop.class.path optional system property points to all JARs or directories that may have classes that are annotated as @Aspect (See Chapter "Annotated Bindings"). JBoss AOP will browse all classes in this path to see if they are annotated. The property can have one or files it points to delimited by the operating systems specific classpath delimiter (';' on windows, ':' on unix).
It is invoked as:
$java -classpath ... [-Djboss.aop.path=...] [-Djboss.aop.class.path=...] \ org.jboss.aop.standalone.Compiler <class files or directories>
In the /bin folder of the distribution we have provided batch/script files to make this easier. It includes all the aop libs for you, so you just have to worry about your files. The usage:
$ aopc15 <classpath> [-aoppath ...] [-aopclasspath ...] [-report] [-verbose] \ <class files or directories>+
The other parameters are the same as above.