PDA

View Full Version : indirectly called method not intercepted


springtester
Sep 1st, 2004, 06:30 AM
Hi, I would like to trace bar(), but only if it gets called from foo(). Unfortunately only if I call bar() directly it will get intercepted. Could somebody help me how to get this working?
Thank you very much in advance.
robert


package test;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
public class TestAOP {
public void foo(){
System.err.println( "foo calling bar" );
bar();
}
public void bar(){
System.err.println( "bar called" );
}
public static void main(String[] args){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/application-context.xml" );
TestAOP aop = (TestAOP)ctx.getBean("testAOPAdvised");
aop.bar();
aop.foo();
}
}

<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugIntercept or">
</bean>
<bean id="testAOP" class="test.TestAOP"/>
<bean id="testAOPAdvised" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="target">
<ref local="testAOP" />
</property>
<property name="interceptorNames">
<list>
<value>debugInterceptor</value>
</list>
</property>
</bean>


---console output----
Debug interceptor: count=1 invocation=[Invocation: method=[public void test.TestAOP.bar()] args=[Ljava.lang.Object;@11eb199] target is of class [test.TestAOP]]
bar called
Debug interceptor: next returned
Debug interceptor: count=2 invocation=[Invocation: method=[public void test.TestAOP.foo()] args=[Ljava.lang.Object;@11eb199] target is of class [test.TestAOP]]
foo calling bar
bar called
Debug interceptor: next returned

So before the last "bar called" I would like to have something like
Debug interceptor: count=3 invocation=[Invocation: method=[public void test.TestAOP.bar()] args=[Ljava.lang.Object;@11eb199] target is of class [test.TestAOP]]

Alef Arendsen
Sep 1st, 2004, 07:01 AM
Spring's AOP framework is based on proxies, which means that once you've passed the proxy and are inside the target, subsequent calls from within the target to methods inside that target won't be intercepted anymore (they don't go through the proxy anymore).

It's possible to expose the proxy in your business object by setting the exposeProxy property on the ProxyFactoryBean to true. Using the AopContext you can retrieve the current proxy (only if you've set exposeProxy to true). Cast it to the interface (or class) you're exposing and call the method you'd like to call. This ties your business object to the AopContext class, but this is minimal IMO.

Alef