I´m trying to use aspectJ with eclipse 4.4.0 (Luna). Y cannot find any ajdt plugin suitable for this version of eclipse.
Anyway i think aspectj should work, so I put the aspectj jars in the lib directory of my webApp, and create this Aspect class:
@Aspect
public class LoggerAspect {
@Before("(execution(* *.*(..))")
public void intercept(JoinPoint jp) {
System.out.print("before");
}
@After("(execution(* *.*(..))")
public void afterReturning(JoinPoint jp) {
System.out.print("after");
}
@AfterThrowing(pointcut = "execution(* *.*(..))")
public void logException(JoinPoint jp, Exception t) {
System.out.print("throwing");
}
}
But nothing is being logged. What i´m missing?
If you have set up the AspectJ Maven Plugin in your pom.xml, then to make that work in Luna, it seems you first need to add the following update site (menu Help, Install New Software):
http://download.eclipse.org/tools/ajdt/44/dev/update/
From that site, install AspectJ Development Tools.
Once installed, you can also install the Maven Integration for AJDT through Preferences, Maven, Discovery, Open Catalog, and then selecting AspectJ m2e Configurator.
(Trying to do the last step before installing the tools, will throw a Missing requirement: AspectJ Development Tools Core 2.1.3. The same error is thrown when trying to install AspectJ things from the default Luna update sites.)
Then try with Kepler if you want to use AJDT. ;-)
BTW, have you compiled your aspect and Java classes with ajc (AspectJ compiler)? Or do you want to compile them with javac? In this case you need to weave the aspects during runtime (LTW, load-time weaving) and need the weaving agent (-javaagent:path/to/aspectjweaver.jar
) plus a corresponding configuration in aop.xml
. After you will have told me what you want to do, I can update this answer with a concrete example.
Update: Okay, I tested your aspect. It contains a few syntax errors and other problems, e.g.
@Before("(execution(* *.*(..))")
there the number parentheses do not match. Instead it should be @Before("execution(* *.*(..))")
.@AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "t")
.Now here is a fully testable stand-alone example:
Driver application:
class Application {
public static void main(String[] args) {
new Application().foo();
}
public void foo() {
try {
sayHello("world");
}
catch (Exception e) {
System.out.println("Caught exception: " + e);
}
}
public void sayHello(String recipient) {
System.out.println("Hello " + recipient + "!");
throw new RuntimeException("Oops!");
}
}
Aspect:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggerAspect {
@Before("execution(* *.*(..))")
public void intercept(JoinPoint jp) {
System.out.println("before " + jp);
}
@After("execution(* *.*(..))")
public void afterReturning(JoinPoint jp) {
System.out.println("after " + jp);
}
@AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "t")
public void logException(JoinPoint jp, Exception t) {
System.out.println("throwing " + jp);
}
}
How to compile & run with ajc:
Assuming that your source files are in a directory named src
and you want the class files to end up in bin
, this is how you compile:
ajc -sourceroots src -1.7 -d bin -cp aspectjrt.jar
Now run the application:
java -cp bin;aspectjrt.jar Application
before execution(void Application.main(String[]))
before execution(void Application.foo())
before execution(void Application.sayHello(String))
Hello world!
after execution(void Application.sayHello(String))
throwing execution(void Application.sayHello(String))
Caught exception: java.lang.RuntimeException: Oops!
after execution(void Application.foo())
after execution(void Application.main(String[]))
How to compile with javac & run with LTW:
You need to compile with debug symbols (-g
):
javac -g -cp aspectjrt.jar -d bin src\*.java
Before you can run the program you need a LTW configuration under bin\META-INF\aop.xml
(or aop-ajc.xml
):
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="LoggerAspect"/>
</aspects>
<weaver>
<include within="*"/>
</weaver>
</aspectj>
Now you can run the application using the weaving agent:
java -javaagent:aspectjweaver.jar -cp bin Application
The output is the same, but if you want to see more you can add other weaver options like
<weaver options="-verbose -showWeaveInfo">
This should yield the following output:
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by before advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by after advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by before advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by after advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by before advice from 'LoggerAspect'(LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by after advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java)
before execution(void Application.main(String[]))
before execution(void Application.foo())
before execution(void Application.sayHello(String))
Hello world!
after execution(void Application.sayHello(String))
throwing execution(void Application.sayHello(String))
Caught exception: java.lang.RuntimeException: Oops!
after execution(void Application.foo())
after execution(void Application.main(String[]))
If you need even more, add the -debug
option to the weaver options, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With