Results 1 to 7 of 7

Thread: exposing the current proxy (exposeProxy)

  1. #1
    Join Date
    Dec 2005
    Location
    California
    Posts
    63

    Default 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.
    Janos

  2. #2
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    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);

  3. #3
    Join Date
    Dec 2005
    Location
    California
    Posts
    63

    Default 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());
    Janos

  4. #4
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    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.

  5. #5
    Join Date
    Dec 2005
    Location
    California
    Posts
    63

    Default 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.
    Janos

  6. #6
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    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.

  7. #7

    Default

    Quote 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?

Posting Permissions

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