Announcement Announcement Module
Collapse
No announcement yet.
indirectly called method not intercepted Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • indirectly called method not intercepted

    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

    Code:
    package test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    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.DebugInterceptor">
    	</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]]

  • #2
    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

    Comment

    Working...
    X