Newbie question - Advice methods not being called...
I'm currently working through Spring In Action 2nd ed., and have got as far as trying to get the basic Spring AOP functionality working - it compiles just fine, and the methods run, BUT, none of the advice methods work. I have a feeling I'm doing something very daft, could someone point it out to me, please?
Here's the class I want to add the advice to:
Code:
package springinaction.springidol;
public class Juggler implements Performer {
private int beanbags;
private Juggler(){}
public void perform() throws PerformanceException
{
System.out.println("Juggling " + this.beanbags + " beanbags.");
}
public int getBeanbags()
{
return beanbags;
}
public void setBeanbags(int beanbags)
{
this.beanbags = beanbags;
}
}
Here's the class that should do things when the advice methods are called:
Code:
package springinaction.springidol;
public class Audience{
public void applaud()
{
System.out.println("CLAP CLAP CLAP");
}
public void demandRefund()
{
System.out.println("Booooooo, we want our money back.");
}
public void takeSeats()
{
System.out.println("The audience is taking their seats");
}
public void turnOffCellPhones()
{
System.out.println("The audience is turning off their cell phones");
}
}
Here's my advice class:
Code:
package springinaction.springidol;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
public class AudienceAdvice implements
MethodBeforeAdvice,
AfterReturningAdvice,
ThrowsAdvice {
private Audience audience;
public AudienceAdvice(){System.out.println("Invoked AudienceAdvice");}
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable
{
audience.takeSeats();
audience.turnOffCellPhones();
}
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable
{
audience.applaud();
}
public void afterThrowing(Throwable throwable)
{
audience.demandRefund();
}
public void setAudience(Audience audience)
{
this.audience = audience;
}
}
and here's my context xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="duke" class="springinaction.springidol.Juggler">
<property name="beanbags" value="3"/>
</bean>
<bean id="audience" class="springinaction.springidol.Audience"/>
<bean id="audienceAdvice" class="springinaction.springidol.AudienceAdvice">
<property name="audience" ref="audience"/>
</bean>
<bean id="audienceAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="audienceAdvice" />
<property name="pattern" value=".*perform" />
</bean>
</beans>
As I understand it, there should be text written to standard out when the perform method of Juggler gets called, but this isn't happening...