Announcement Announcement Module
Collapse
No announcement yet.
exposing the current proxy (exposeProxy) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • exposing the current proxy (exposeProxy)

    How do I do the following with the <aop: > namespace or annotations?

    Code:
    <property name="exposeProxy">
    <value>true</value>
    </property>
    For example, what do I need to modify if my object has this on a method:
    Code:
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void blah() ...
    if I want to call blah from an other method in the same bean using
    Code:
    ((MyInterface) AopContext.currentProxy()).blah();
    Any input is appreciated.

  • #2
    The aop.xsd doesn't expose "exposeProxy". Maybe there is a better (preferred) way but here is one way.

    Get a hold of the application context with <aop:config ...> (e.g. implement ApplicationContextAware) and then do:

    Code:
    String beanName = "org.springframework.aop.config.internalAutoProxyCreator";
    AspectJAwareAdvisorAutoProxyCreator autoProxyCreatory =
        (AspectJAwareAdvisorAutoProxyCreator) getApplicationContext().getBean(beanName);
    autoProxyCreatory.setExposeProxy(true);

    Comment


    • #3
      thanks

      Obviously this has to be this complicated because it is not a good practice.
      What I did was I got hold of the current bean from the bean factory and then I did not even need to expose the proxy. I called the method on the returned proxy not on "this":

      Code:
      // get "this" currently executing bean
      packageResolver = (PackageResolving) beanFactory.getBean("packageResolver");
      // do not call on "this"
      // resolvePackagesForDealer(dealership);
      packageResolver.resolvePackagesForDealer(dealership.getDealerCode());

      Comment


      • #4
        That's much cleaner. Ofcouse if you could arrange your logic so the advice can make that 2nd call then you wouldn't even need to get the bean factory.

        Comment


        • #5
          2nd call

          Yes, I think the advice could make the second call, but then it would not be an advice - it would know too much about the advised object.
          Do you have a good example of when an advised object needs its proxy?
          An other solution would be to use two service objects.
          The reason why I need a new transaction in the 2nd method is that I loop through many objects and write them back to the database, and so the transaction would be very large and some vendors can not handle 10,000+ updates in one.

          Comment


          • #6
            An other solution would be to use two service objects.
            The reason why I need a new transaction in the 2nd method is that I loop through many objects and write them back to the database, and so the transaction would be very large and some vendors can not handle 10,000+ updates in one.?
            Without knowing much about the structure of your classes I would say you could look into separating the looping logic out into a separate class, which would control how the updates are split into batches.

            Comment


            • #7
              Originally posted by mucsij View Post
              For example, what do I need to modify if my object has this on a method:
              Code:
              @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
              public void blah() ...
              if I want to call blah from an other method in the same bean using
              Code:
              ((MyInterface) AopContext.currentProxy()).blah();
              I believe, tricks described in this thread do not work for unit tests, as Spring Test infrastructure does not create AOP proxies around unit tests (but does @Transactional annotation processing "manually"). Any ideas how to hack in this case?

              Comment

              Working...
              X