Search code examples
springhibernateaopaspectjaspect

Problem with AOP/AspectJ not executing on criterion


Does anyone know why the following AOP/AspectJ doesn't work in Examples 2,3 ?

Note: Example 1 works.

My goal is to intercept the firing of Hibernate Query's executeUpdate(), which occurs throughout the application. Hibernate Query is an interface, and I see in the code that the implementing class I get back is QueryImpl. So that is the class I want to target, although I've tried generic filters too.

XML

<aop:config>
    <aop:aspect id="myAspect" ref="aspectBean">

       <!-- EXAMPLE 1: SIMPLE TEST: WORKS OK. 
            I intercept all methods in all my custom classes in "dao" package. -->
        <aop:pointcut id="test1" expression="execution(* myapp.dao.*.*(..))" /> 
        <aop:before pointcut-ref="test1" method="doTest1" /> 

       <!-- EXAMPLE 2: DOESN'T WORK.
            Target everything in Hibernate's Impl package with executeUpdate() function -->         
        <aop:pointcut id="executeUpdate2" expression="execution(* org.hibernate.impl..*..executeUpdate(..))" />
        <aop:before pointcut-ref="executeUpdate2" method="handleExecuteUpdate" /> 

        <!-- EXAMPLE 3: DOESN'T WORK.
            Target QueryImpl specifically -->   
        <aop:pointcut id="executeUpdate3" expression="execution(* org.hibernate.impl.QueryImpl.executeUpdate(..))" /> 
        <aop:before pointcut-ref="executeUpdate3" method="handleExecuteUpdate" /> 

    </aop:aspect>
</aop:config>
<bean id="aspectBean" class="myapp.util.AOPAspect">
</bean>

I know for a fact the Hibernate hierarchy is correct. Example #1 works great so I know the AOP/AspectJ is wired properly. Is there something about external library traversal that doesn't support AOP/AspectJ?


Solution

  • I found out it doesn't work because I'm dealing with an external JAR (in this case, Hibernate). Example 1 works because I'm within my own code.

    No easy solutions for external JAR pointcuts, only Load-Time Weaving is possible (but I haven't tried it),

    Aspectj: intercept method from external jar