Announcement Announcement Module
Collapse
No announcement yet.
interceptors for method call... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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?

      Comment


      • #4
        Did you set the "this" property with the proxy instance?

        Regards,
        Andreas

        Comment


        • #5
          ...

          I've got no luck for days...

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

          Comment


          • #6
            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();
              }
            }

            Comment

            Working...
            X