Mar 13th, 2008, 07:10 PM
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());
<bean id="simpleAdvice" class="com.pwg.tradedemo.aspects.logging.BasicLogg ingAdvice" />
<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.
Mar 14th, 2008, 06:03 AM
Are you sure your pointcut points to the correct class and you use that class when calling your methods ?
Mar 14th, 2008, 08:03 AM
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.
Mar 14th, 2008, 08:59 AM
Assuming JpaTradesDao is an interface, does getTrades method exist in it ?
Can you post, please, the interface, implementing class and the test case ?
Mar 14th, 2008, 09:18 AM
Figured it out
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.
Thanks for all of your help Andrei.