Results 1 to 6 of 6

Thread: interceptors for method call...

  1. #1
    Join Date
    Jun 2005
    Posts
    17

    Question interceptors for method call...

    I setup a pointcut that intercepts the method calls beginning with create as follow...

    Code:
    	<bean id="shipmentService" parent="baseTransactionProxy">
    		<property name="target">
    			<bean class="serviceBean.impl.ShipmentServiceTarget">
    				<property name="shipmentMaintainDao"><ref bean="shipmentMaintainDao"/></property>
    			</bean>
    		</property>
    		<property name="preInterceptors"> 
    			<list>
    				<ref bean="shipmentCreationPreTaskAdvisor"/>
    			</list>
    		</property>
    	</bean>
    
    	<bean id="shipmentCreationPreTaskAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> 
    		<property name="advice">
    			<ref local="shipmentCreationPreTaskAdvice"/> 
    		</property> 
            <property name="patterns"> 
                <value>.*(create).*</value>
            </property>
        </bean>
    The pointcut works fine if the call is from another bean.
    e.g. getShipmentService().createShipment

    however, it cannot capture for call that trigger within the shipmentService bean,
    e.g. this.createShipment()

    How can the pointcut capture the calls triggered within the bean?

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Due to the proxy delegation model this is not possible.

    You might have a look at this thread for more information. I also proposed a workaround there.

    Not sure however, if you might have more success using AspectJ on this (haven't tried it myself yet).


    Regards,
    Andreas

  3. #3
    Join Date
    Jun 2005
    Posts
    17

    Default great ... thx

    I prefer the getThis() over the AopContext approach as the bean is "less" bounded to the spring framework.
    But I have no luck to get it done.

    I put following into my bean:

    Code:
    MyBean thisInstance = this;
    MyBean getThis() {
    	return thisInstance;
    }
    void setThis(MyBean instance) {
    	thisInstance = instance;
    }
    then tried calls like getThis().method2()

    it seems the method2 can't be captured by the pointcut...

    Am I missing something?

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Did you set the "this" property with the proxy instance?

    Regards,
    Andreas

  5. #5
    Join Date
    Jun 2005
    Posts
    17

    Default ...

    I've got no luck for days...

    Could you show me an example how to set the proxy instance into "this"?

  6. #6
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Here is a sample application context injecting the proxy into the target instance:

    Code:
    <beans>
      <bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetName" value="fooTarget"/>
        <property name="interceptorNames">
          <list>
            <value>interceptor</value>
          </list>
        </property>
      </bean>
    
      <bean id="interceptor" class="org.springframework.aop.interceptor.SimpleTraceInterceptor">
      </bean>
    
      <bean id="fooTarget" class="test.spring.Foo">
        <property name="this" ref="foo"/>
      </bean>
    
    </beans>
    And this is the class:
    Code:
    package test.spring;
    
    public class Foo {
    
      private Foo thisInstance = this;
    
      
      public Foo() {}
    
    
      public Foo getThis() {
        return this.thisInstance;
      }
    
      public void setThis(Foo thisInstance) {
        this.thisInstance = thisInstance;
      }
    
      public void op1() {
        System.out.println("op1");
      }
      
      public void op2() {
        System.out.println("op2 calling op1");
        getThis().op1();
      }
    }

Posting Permissions

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