Results 1 to 5 of 5

Thread: Basic Spring AOP

  1. #1
    Join Date
    Mar 2008
    Posts
    9

    Default Basic Spring AOP

    I'm just getting started with Spring AOP and I'm trying to add some simple logging into my web app. For logging purposes I want to implement the MethodBeforeAdvice, and AfterReturningAdvice so that I have access to the method names that will be called. To get things started I am trying to add logging to my Dao. My advice class looks like this:

    public class BasicLoggingAdvice implements
    MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice
    {

    public void before(Method method, Object[] params, Object target) throws Throwable
    {
    System.out.println("Entered method: " + method.getName());
    }

    public void afterReturning(Object retVal, Method method, Object[] params, Object target) throws Throwable
    {
    System.out.println("Exited method: " + method.getName());
    }

    public void afterThrowing(Method method, Object[] params, Object target, Throwable e)
    {
    System.out.println("Exception occurred: " + e.getMessage());
    }
    }


    Spring configuration:

    <bean id="simpleAdvice" class="com.pwg.tradedemo.aspects.logging.BasicLogg ingAdvice" />

    <bean id="simpleAdvisor"
    class="org.springframework.aop.aspectj.AspectJExpr essionPointcutAdvisor"
    p:expression="execution(* com.pwg.tradedemo.dal.jpa.JpaTradesDao.*(..))"
    p:advice-ref="simpleAdvice"/>

    <bean class="org.springframework.aop.framework.autoproxy .DefaultAdvisorAutoProxyCreator"/>

    It seems like my problem is with my pointcut, when I use "execution(* *.getTrades())" everything seems to work fine. I'm not receiving any exceptions, I'm just not getting any results. Any advice is greatly appreciated.

  2. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    Are you sure your pointcut points to the correct class and you use that class when calling your methods ?

  3. #3
    Join Date
    Mar 2008
    Posts
    9

    Default Class name is correct

    Thanks for the reply Andrei. I double checked the class and package name and I am positive they are correct. I know that the class is getting used because it is my only dao and I am receiving data from the database.

    If any other information would help just let me know.

    Shane

  4. #4
    Join Date
    Mar 2007
    Posts
    515

    Default

    Assuming JpaTradesDao is an interface, does getTrades method exist in it ?
    Can you post, please, the interface, implementing class and the test case ?

  5. #5
    Join Date
    Mar 2008
    Posts
    9

    Default Figured it out

    Andrei,

    Your last post made it "click" for me. JpaTradesDao is a concrete class for TradesDao. I was calling getTrades through the TradesDao but I had my pointcut set up for JpaTradesDao. I didn't realize that would make a difference. It's awesome that you can set up a pointcut for an interface (Spring is SWEET).

    Here is what I changed by bean definition to.

    <bean id="simpleAdvisor"
    class="org.springframework.aop.aspectj.AspectJExpr essionPointcutAdvisor"
    p:expression="execution(* com.pwg.tradedemo.dao.TradesDao.*(..))"
    p:advice-ref="simpleAdvice"/>

    Thanks for all of your help Andrei.

    Shane

Posting Permissions

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