[jboss-user] [JBoss AOP] - PER_INSTANCE and constructor executions, method calls, etc.
paulfreeman
do-not-reply at jboss.com
Fri Jul 27 14:09:02 EDT 2007
I seem to be having an issue with the PER_INSTANCE scope. I have a simple aspect that captures the execution of a method and the execution of a constructor on the same object:
package com.kronos.jboss.aop;
import org.jboss.aop.Aspect;
import org.jboss.aop.Bind;
import org.jboss.aop.PointcutDef;
import org.jboss.aop.advice.Scope;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.aop.pointcut.Pointcut;
@Aspect(scope=Scope.PER_INSTANCE)
public class AAspect {
public AAspect(){
System.out.println("** CREATED " + this.getClass().getSimpleName());
}
@PointcutDef ("execution($instanceof{com.kronos.jboss.aop.A}->new(..))")
public static Pointcut newA;
@PointcutDef ("execution(* $instanceof{com.kronos.jboss.aop.A}->foo(..))")
public static Pointcut aroundFooMethod;
@Bind (pointcut="com.kronos.jboss.aop.AAspect.newA")
public Object aroundNewA(Invocation invocation) throws Throwable{
System.out.println("before new A " + this.getClass().getSimpleName());
Object result = invocation.invokeNext();
System.out.println("after new A " + this.getClass().getSimpleName());
return result;
}
@Bind (pointcut="com.kronos.jboss.aop.AAspect.aroundFooMethod")
public Object aroundFoo(Invocation invocation) throws Throwable{
System.out.println("before foo " + this.getClass().getSimpleName());
Object result = invocation.invokeNext();
System.out.println("after foo " + this.getClass().getSimpleName());
return result;
}
}
package com.kronos.jboss.aop;
public class A {
public A(){
System.out.println("In new A...");
}
public void foo(){
System.out.println("In foo...");
}
}
When I execute a simple test:
package com.kronos.jboss.aop;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
a.foo();
}
}
I expect to have the AAspect instance created every time an instance of A is created, however the advice on the A constructor is never executed. See output:
In new A...
** CREATED AAspect
before foo AAspect
In foo...
after foo AAspect
If I change the scope to PER_CLASS, I get the expected output, but not the desired number of instances of my aspect. Here is the output:
** CREATED AAspect
before new A AAspect
In new A...
after new A AAspect
before foo AAspect
In foo...
after foo AAspect
Also, if I the scope is PER_INSTANCE and I change the foo method pointcut definition from exection to call, an instance of my AAspect is never created. Here is the modified pointcut:
@PointcutDef ("call(* $instanceof{com.kronos.jboss.aop.A}->foo(..))")
public static Pointcut aroundFooMethod;
Here is the output:
In new A...
In foo...
If I try call in the pointcut and change the scope back to PER_CLASS, I get the correct output:
** CREATED AAspect
before new A AAspect
In new A...
after new A AAspect
** CREATED AAspect
before foo AAspect
In foo...
after foo AAspect
I assume this is a bug, but I wanted to check here first to make sure I understand the PER_INSTANCE scope designation. I was unable to find this listed as a BUG in JIRA. Any help would be appreciated.
I have tried this both in the annotation syntax shown above, and with an aop.xml file configuration - same results.
Thanks,
Paul
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4068319#4068319
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4068319
More information about the jboss-user
mailing list