Announcement Announcement Module
No announcement yet.
Obtaining Advised Object from Within the Interceptor Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Obtaining Advised Object from Within the Interceptor

    Howdy folks,

    I'm writing a largely generic persistence mechanism basing it on Spring AOP. The desire is to have business object logic completely independent of the persistence mechanism, but for it to support serializing objects out to XML files for persistent storage, to read stub objects at start-up, and to do a one-off lazy read each time anything other than display properties are needed.

    The core guts of this is a PersistenceMixin method interceptor, and a PersistentEntity interface. Any object which can be saved is wrapped with an interceptor chain including the persistence mixin (a stateful mixin that holds onto the underlying object's ID). Then clients who need to save can cast the wrapped object to PersistentEntity and call save().

    Save() in the method interceptor will go to the PersistentStore (injected by spring) which takes care of persisting objects. This store requires an ID and a Description. The ID is managed by the PersistentStore, and is assosciated with individual objects through the Mixin. In order for this all to work right, the PersistentStore needs to get references to the business objects with their full interceptor chain.

    The problem is I need to get ahold of the PersistentEntity, the wrapped object and it's interceptor chain, in the mixin's invoke. I can call getThis() on the MethodInvocation and get the target, I can refer to this and get the instance of the Mixin advice itself, but I can't locate the proxied object which wraps the target.

    I could solve this by handing the aop wrapped entities to the store from the clients instead of having clients cast to persistententity and save as an alternative, but I felt that the cast to PersistentEntity and call to save() was a bit more elegant. Is it possible to get ahold of the AOP wrapped object during the course of the interceptor chain?

    Thanks in advance!

  • #2
    Take a look at org.springframework.aop.framework.AopContext and org.springframework.aop.framework.ProxyConfig#setE xposeProxy(boolean).


    • #3
      Thanks much, very helpful!


      • #4
        not sure where its failing

        I need to be able to access the original object that is being proxied and I've tried to use AopContext.currentProxy(), but it throws an exception with Spring 1.2. Here is a snippet of my config file

        <bean id="test"
        class="org.springframework.aop.framework.ProxyFact oryBean">
        <property name="proxyTargetClass">
        <property name="singleton">
        <property name="exposeProxy">
        <property name="proxyInterfaces">
        <property name="interceptorNames">
        <property name="target">
        <ref bean="testTarget" />

        The intrcoduction works and I cast to the additional interface, but can't get back to the class!


        • #5

          sorry, it works if the threadlocal call is in the interceptor, but i placed the call in a junit test and it failed. by refactoring the code, it all worked fine :$