Getting started with Autoproxy @AspectJ
I'm sorry. I know this must be answered already on this forum, but I can't seem to find it with search. I am working through Spring in Action 2nd Edition and using Spring 2.5. I have gotten advice working by using proxy factory beans, but I can't seem to get any of my AutoProxy stuff working. In particular, I'd like to use the AspectJ annotations. By "not working" I mean the Audience methods aren't being called at all. I thought including the <aop:aspectj-autoproxy/> tag would do the trick, but I guess not. Here is my code:
problem-config.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<aop:aspectj-autoproxy/>
<bean id="duke" class="com.dbo2.idol.Juggler">
<property name="beanBags" value="15" />
</bean>
<bean id="audience" class="com.dbo2.idol.Audience" />
</beans>
Performer.java:
Code:
package com.dbo2.idol;
public interface Performer {
void perform() throws PerformanceException;
}
Juggler.java:
Code:
package com.dbo2.idol;
public class Juggler implements Performer {
private int beanBags = 3;
public void setBeanBags(int beanBags) {
this.beanBags = beanBags;
}
@Override
public void perform() throws PerformanceException {
System.out.println("JUGGLING " + beanBags + " BEANBAGS");
}
}
Audience.java:
Code:
package com.dbo2.idol;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Audience {
@Pointcut("execution(* *.perform(..))")
public void performance() {
}
@Before("performance()")
public void takeSeats() {
System.out.println("The audience is taking their seats");
}
@Before("performance()")
public void turnOffcellPhones() {
System.out.println("The audience is turning off their cell phones");
}
@AfterReturning("performance()")
public void applaud() {
System.out.println("CLAP CLAP CLAP CLAP CLAP");
}
@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("Boo! We want our money back!");
}
}
ProblemApp.java:
Code:
package com.dbo2.idol;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class ProblemApp {
/**
* @param args
*/
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"C:/Dev/SafetyNet/WebContent/WEB-INF/problem-config.xml"));
Performer performer = (Performer) factory.getBean("duke");
performer.perform();
}
}
When I run this I get the following output:
Quote:
2008-05-18 22:09:20,517 INFO org.springframework.beans.factory.xml.XmlBeanDefin itionReader - Loading XML bean definitions from file [C:\Dev\SafetyNet\WebContent\WEB-INF\problem-config.xml]
JUGGLING 15 BEANBAGS
I expected some stuff from my Audience, but nothing. Any advice on what I'm doing wrong? Am I making a bad assumption about the <aop:aspectj-autoproxy/> tag? Do I need to pass any JVM arguments to take advantage of the annotations? (I'm using JDK 1.6.)