Author
AOP in Apring 2.5 - Declare Aspects with AspectJ annotations
Sam Gehouse
Ranch Hand
Joined: Jul 21, 2003
Messages: 264
posted Jul 24, 2008 13:07:00
I am using AOP in Spring 2.5. I am trying to declare Aspects with AspectJ annotation. I do not see the methods annotated as @Before and @After getting executed. Following is the driver class: package org.poc.aop.annotation; /** * From Spring in Action book. */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AudienceTest { public static void main(String args[]) { ApplicationContext ctx = new ClassPathXmlApplicationContext("aop_annotation_audience.xml"); Performing performance = (Performing) ctx.getBean("perfBean"); performance.perform(); } } Following is the class which has a perform() method around which, advices will be thrown: package org.poc.aop.annotation; public class Performing { public void perform(){ System.out.println("Inside performing"); } } Following is the POJO which is annotated with advice: package org.poc.aop.annotation; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Before; /** * * Needs the jars that are under lib/aspectj and lib/asm * */ public class Audience { @Before("execution(* org.poc.aop.annotation.Performing.perform(..))") public void takeSeat(){ System.out.println("take seat"); } @After("execution(* org.poc.aop.annotation.Performing.perform(..))") public void applaud(){ System.out.println("applaud"); } } Following is the aop_annotation_audience.xml: <?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.5.xsd"> <aop:aspectj-autoproxy/> <bean id="audience" class="org.poc.aop.annotation.Audience" /> <bean id="perfBean" class="org.poc.aop.annotation.Performing" /> </beans> I see the print line in the console as: Inside performance. I do not see "take seat" being written in console, before "Inside Performance" I do not see "applaud" getting written in console either. As such, Performing.perform() method gets executed. But the take Audience.takeSeat() method does not get executed before Performing.perform(). Audience.applaud() method does not get executed after Performing.perform() either.
Dean Pullen
Ranch Hand
Joined: May 30, 2003
Messages: 56
posted Jan 19, 2010 04:03:51
An old one but found via google when I was searching for the same thing.
I hadn't declared my aspect as a bean within the config file (or used @Component) so it wasn't scanned.
However, you appear to have missed @Aspect at the top of your Audience class.
@Aspect
public class Audience { .. }