Results 1 to 2 of 2

Thread: indirectly called method not intercepted

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. PerformanceMonitorInterceptor
    By tnist in forum AOP
    Replies: 3
    Last Post: Aug 24th, 2005, 01:39 PM
  5. Replies: 8
    Last Post: Dec 7th, 2004, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •