Announcement Announcement Module
No announcement yet.
"this" reference doesn't point to proxy Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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):

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

    [org.springframework.transaction.interceptor.TransactionInterceptor] - Getting transaction for [DicomFeed.importImage]
    [DicomFeed] - DicomFeed#importImage: I am [email protected]
    [org.springframework.transaction.interceptor.TransactionInterceptor] - Completing transaction for [DicomFeed.importImage]
    [DicomFeed] - DicomFeed#txTest: I am [email protected]
    [DicomFeed] - DicomFeed#importImage: I am [email protected]
    The stack trace indicates that the CGLIB-generated proxy (subclass of DicomFeed) is a different instance than the un-proxied instance ([email protected]). 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:

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


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