Another thing is that if we could limit ourselves to Log4j instead of commons-logging, we could achieve the same with less effort using a custom formatter and perhaps Log4j’s Nested Diagnostic Context.
The code is basically quite simple – we have one Aspect J aspect using the annotation-style, which intercepts calls to commons-logging’s info(Object) and info(Object, Throwable) and re-formatts the message before handing it over to the actual logger.
Note: I had to use ajc even though I was using the pure java 5 syntax based on annotations instead of the legacy Aspect J syntax due to bug in Aspect J (fixed in v. First I tried the Aspect J Maven plugin but it didn’t work for me because I had a special need – a project containing only an aspect while the plugin requires, if I remember correctly, also the advised sources to be in the project.
A fix is available and should be included in the version 1.3 but the project doesn’t seem to be very active so who knows when it will be released. Next I tried the maven-compiler-plugin, which supports various back-ends including Aspect J with the dependency on plexus-compiler-aspectj and compiler Id set to aspectj.
My Library Project Before Aspect J 5 this was the only option and it is still more powerful and has better support of AJDT (according to the FAQ).
Basically you write the aspect in an extended Java syntax in an file and compile it with a special Aspect J compiler – though this is done behind the scene for you thanks to having created an Aspect J project, which also provides you with a special Aspect J/Java editor for and aspect files.
This post is aimed at giving you the knowledge that you may (or I should rather say “will”) need and at persuading you that learning basics of code injection is really worth the little of your time that it takes.
I’ll present three different real-world cases where code injection came to my rescue, solving each one with a different tool, fitting best the constraints at hand.
i think the eclipse 3.1 comes built-in with jdk1.4. How do i configure my eclipse to make use of jdk1.5 (my ecliple is installed in d:\ and jdk1.5 in c:\) D:\eclipse\-vm C:\Program Files\Java\jdk1.5.0_06\jre\bin\javaw -vmargs -Xmx512M -Xms256M This is the command i used in the target section on eclipse3.1 i got an error (jre or jdk must be avaliable to run eclipse)Thanks for the reply Jay, The installed jre that is displayed on the window(window/prefrences/java/installed)is C:\Program Files\Java\jre1.5.0_06.I’m only learning Aspect J, so the solution may be not and likely isn’t the best one. Since Aspect J 5 we have two options – either using the older custom Aspect J syntax with Aspect J compiler or using annotations. First, we have a Java project with the junit test that we want to run with the timing aspect.It depends on another project with the actual class and method whose performance we want to measure.There are also other AOP tools for Java but it seems that Aspect J is the most wide-spread one, especially since its merge with Aspect Werkz.I used to dislike it because it required a special extended Java syntax and compiler but this isn’t true anymnore (more on that later). For this I’ll use: My goal is to create an aspect invoked around the method in question that will measure the time it takes for the method to execute, log it, and compute and log the average execution time.It doesn’t help to set the envrionmental variable JAVA_HOME to point to the JDK because Maven resets it to $JAVA_HOME/jre in the case – see MANTRUN-91.