[JBoss AOP] - PER_INSTANCE and constructor executions, method calls, etc.
by paulfreeman
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
18Â years, 9Â months
[JBoss Seam] - Re: Seam on tomcat connection problem using EJB
by Stateless Bean
F**** somethink wrong is going on when I insecrt source of jboss-beans. :(
third try
| <?xml version="1.0" encoding="UTF-8"?>
|
| <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd"
| xmlns="urn:jboss:bean-deployer">
|
| <bean name="CenturionDatasourceBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
| <property name="driverClass">org.postgresql.Driver</property>
| <property name="connectionURL">jdbc:postgresql://localhost:5432/Centurion</property>
| <property name="userName">postgres</property>
| <property name="jndiName">java:/CenturionDatasource</property>
| <property name="minSize">0</property>
| <property name="maxSize">10</property>
| <property name="blockingTimeout">1000</property>
| <property name="idleTimeout">100000</property>
| <property name="transactionManager"><inject bean="TransactionManager"/></property>
| <property name="cachedConnectionManager"><inject bean="CachedConnectionManager"/></property>
| <property name="initialContextProperties"><inject bean="InitialContextProperties"/></property>
| </bean>
|
| <bean name="CenturionDatasource" class="java.lang.Object">
| <constructor factoryMethod="getDatasource">
| <factory bean="CenturionDatasourceBootstrap"/>
| </constructor>
| </bean>
|
| </deployment>
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4068314#4068314
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4068314
18Â years, 9Â months