Announcement Announcement Module
Collapse
No announcement yet.
"this" reference doesn't point to proxy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • "this" reference doesn't point to proxy

    It looks like if you call a method proxied by Spring AOP from another method of the same class (i.e. using "this.theProxiedMethod()"), that call is routed to theProxiedMethod directly rather than going through the AOP interceptor. Apparently this is true for both CGLIB-proxied beans and java.lang.Proxy-based ones. I noticed this when using Spring's annotation-based transaction demarcation, e.g. (example for a CGLIB-based method interception):

    Code:
    public class DicomFeed {
    
      @Transactional(propagation=Propagation.REQUIRED, readOnly=false)
      public void importImage(DicomObject composite, CatchupContext ctx) {
        logger.info("DicomFeed#importImage: I am " + this);
      }
    
      public void txTest() {
        logger.info("DicomFeed#txTest: I am " + this);
        importImage(null, null);
      }
    
    }
    
    ....
    
    DicomFeed dicomFeed = (DicomFeed)context.getBean("dicomFeed");
    dicomFeed.importImage(null, null);
    dicomFeed.txTest();
    Outputs:

    Code:
    [org.springframework.transaction.interceptor.TransactionInterceptor] - Getting transaction for [DicomFeed.importImage]
    [DicomFeed] - DicomFeed#importImage: I am DicomFeed@86f716
    [org.springframework.transaction.interceptor.TransactionInterceptor] - Completing transaction for [DicomFeed.importImage]
    [DicomFeed] - DicomFeed#txTest: I am DicomFeed@86f716
    [DicomFeed] - DicomFeed#importImage: I am DicomFeed@86f716
    The stack trace indicates that the CGLIB-generated proxy (subclass of DicomFeed) is a different instance than the un-proxied instance (DicomFeed@86f716). Is this a necessity? Or was this done for compatibility reasons (so things like this.getClass() work as expected)?

  • #2
    Re: "this" reference doesn't point to proxy

    N.B.: Replacing "this" references with an additionally defined "this_", injected as:

    Code:
    <bean id="dicomFeed" class="DicomFeed>
      <property name="this_" ref="dicomFeed" />
    </bean>
    ...seems to do away with the problem (at the expense of introducing awkwardness into the code).

    Comment


    • #3
      This has nothing to do with spring but rather with the fact that spring uses proxies. (Check chapter 6 of the reference guide). Actually with EJB's you have the same issue you will have to get a reference to the bean and call a method on that (else it won't pass through the proxy and be invoked directly).

      So it is one of the expenses/drawbacks of using proxy based AOP.

      Comment

      Working...
      X