• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

AOP in Apring 2.5 - Declare Aspects with AspectJ annotations

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 { .. }
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!