-
Mar 13th, 2008, 07:10 PM
#1
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.
-
Mar 14th, 2008, 06:03 AM
#2
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
#3
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
-
Mar 14th, 2008, 08:59 AM
#4
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
#5
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
-
Forum Rules