Announcement Announcement Module
Collapse
No announcement yet.
Adding AOP advice for Transaction Management - Proper creation of Proxy? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding AOP advice for Transaction Management - Proper creation of Proxy?

    Hi,
    I am adding <aop:config> to an application which I've upgraded to Spring 2.5 schema but am having Proxy cast issues come up when I run my unit test for Transaction management (using tx:advice and aop:config setting pointcuts. I think I have a set up issue in that I am still needing a declaration or the right jar file or something.

    1. added spring's tx, context, aop jar's to the spring 2.5 jar in our lib
    2. added aop, tx, context schema's
    3. declared transaction managers
    4. declared tx advice/attributes/methods
    5. added a test aop advice and pointcut to execute transactional methods in a dao which collaborates with SqlMapClientTemplate, SqlMapExecutor, etc,

    I am stumped on how to solve proper creation of the auto proxy which seems to be needed.

    1. My first pass at solving was to add aspectweaver.jar to the lib
    2. that got me farther in my tx unit test, but didn't solve the overall issue.
    3. so I declared this auto proxy bean to my context:
    <bean class="org.springframework.aop.framework.autoproxy .DefaultAdvisorAutoProxyCreator"/>

    And now my error is
    java.lang.ClassCastException: $Proxy0 cannot be cast to {com...my dao class}
    at com...TransactionMgmtTest.setUp(TransactionMgmtTes t.java:55)
    at com.intellij.rt.execution.junit.JUnitStarter.main( JUnitStarter.java:40)

    I just read in Ramnivas's blog post: http://blog.interface21.com/main/200...-bean-pointcut
    "Earlier, you could use BeanNameAutoProxyCreator to achieve a similar result; however, that mechanism didn't work with Schema-style or @AspectJ aspects."

    which leads me to believe that since I am using schemas, DefaultAdvisorAutoProxyCreator will not solve my Proxy cast issue. If anyone knows I would really appreciate the insight

    thanks
    Holly
    Last edited by Helena; Dec 29th, 2007, 03:14 PM. Reason: learned that BeanNameAutoProxyCreator won't work

  • #2
    I encountered this problem in our shop, typically people declare an interface, as in public interface MyDao. Then an implementation, MyDaoImpl for example.

    In your unit tests are you
    Code:
    public setMyDaoImpl(MyDaoImpl mdi) {
        myDaoImpl = mdi;
    }
    Since spring is returning a Proxy, it is returning an object that implements MyDao, rather than MyDaoImpl itself.

    We aren't using AspectJ itself, so we are receiving dynamic proxies. You cannot cast a proxy implementing an interface to a concrete class declaration.

    Comment


    • #3
      fixed - aop setup complete (and easy!)

      I honestly don't know what I did but my unit tests were successful the following day - hits of my new schema-based aop new transactional aspects were successful. I am not sure what the proxy issue was, in effort to help anyone finding this error message in the future except to say keep at it, reboot your IDE, troubleshoot anything it could be

      Comment


      • #4
        See my blog post about Spring AOP. I guess you added a proxy-target-class="true" or work with interfaces now.

        Joerg

        Comment


        • #5
          proxy-target-class

          thank you for this explicit tip from your blog. Yes, that is exactly what I did - finally write an interface for our app and wire it in appropriately.

          I am now trying to figure out the benefit or case where doing something like,
          <aop:config proxy-target-class="true">
          <aop:advisor id="transactionManagementAdvisor"
          advice-ref="txAdvice" pointcut-ref="daoOperation"/>
          </aop:config>

          is the best implementation if the proxy declaration will
          1. Override the default JDK dynamic proxies
          2. Force use of CGLIB on all aop advice
          3. Add unnecessary overhead

          I noticed in the carPlant example posted by alef from TSE, that he uses proxy-target-class="true" but am unsure of the case where this is needed....

          Comment


          • #6
            Originally posted by holly View Post
            I noticed in the carPlant example posted by alef from TSE, that he uses proxy-target-class="true" but am unsure of the case where this is needed....
            It is necessary wherever you have to work with the class - either because you don't have an interface at all or need access to specific implementation details. Both might be hints on design problems though.

            Joerg

            Comment


            • #7
              interface implementation for proxy

              It is necessary wherever you have to work with the class - either because you don't have an interface at all or need access to specific implementation details. Both might be hints on design problems though.
              Apologies, but what is necessary? I just wasn't sure what you are targeting as "it" and know it will help me/others.

              kind regards,
              h

              Comment


              • #8
                Originally posted by holly View Post
                Apologies, but what is necessary? I just wasn't sure what you are targeting as "it" and know it will help me/others.
                The "thing" you actually asked for

                Ok, serious. See what I quoted from your post in my post above. You asked for the use cases of proxy-target-class="true". At least that was what I understood.

                Joerg

                Comment


                • #9
                  thankyou !!

                  Comment


                  • #10
                    Hi Jörg,

                    very useful Thread. ;-)
                    Is it possible to use both, Spring AOP and Cglib?
                    We want to use declarative AOP.
                    Any experience?

                    thx

                    Comment


                    • #11
                      You might want to read Jörg blog.

                      Spring AOP already uses CGLib, if you enable class proxying (proxy-target-class='true') cglib proxies will be created.

                      Comment


                      • #12
                        Hi,

                        I read and understand the blog from Jörg. Sorry, my previous question was not clear enough.

                        What I actually meant is, if it is possible to mix it up a) JDK dynamic proxies and b) CGLib proxies per configuration?
                        currently I switch it with the <aop:aspectj-autoproxy proxy-target-class="true/false" /> Tag in my context xml.

                        Let say in 5-10% of my use cases it's better to use Class related and in the rest of my use cases it is better to use the Interface related JDK dynamic proxies that is faster and better regarding the application design.
                        I would not like to use CGLib proxies for the whole spring application due to that 5% of classes better to "proxy" with CGLib proxies.


                        thx
                        Last edited by jackkilian; Jan 10th, 2008, 04:48 PM.

                        Comment


                        • #13
                          Originally posted by jackkilian View Post
                          What I actually meant is, if it is possible to mix it up a) JDK dynamic proxies and b) CGLib proxies per configuration?
                          I did neither get your question from your first post since you referred to Spring AOP as "alternative" to CGLib while it were the dynamic proxies you meant.

                          Unfortunately, the answer is no: SPR-3665.

                          Joerg

                          Comment

                          Working...
                          X