The moose likes Other Application Frameworks and the fly likes AOP in Apring 2.5 - Declare Aspects with AspectJ annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Application Frameworks » Other Application Frameworks
Reply Bookmark "AOP in Apring 2.5 - Declare Aspects with AspectJ annotations" Watch "AOP in Apring 2.5 - Declare Aspects with AspectJ annotations" New topic
Author

AOP in Apring 2.5 - Declare Aspects with AspectJ annotations

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 280
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
Posts: 56
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 { .. }
 
 
subject: AOP in Apring 2.5 - Declare Aspects with AspectJ annotations
 
developer file tools