This week's giveaway is in the Spring forum.
We're giving away four copies of liveProject: Protecting User Data with Spring Security and OAuth2 and have Laurentiu Spilca on-line!
See this thread for details.
Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

AOP in Apring 2.5 - Declare Aspects with AspectJ annotations

Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

public class AudienceTest {
public static void main(String args[]) {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("aop_annotation_audience.xml");
Performing performance = (Performing) ctx.getBean("perfBean");

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(){

Following is the aop_annotation_audience.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""


<bean id="audience" class="org.poc.aop.annotation.Audience" />
<bean id="perfBean" class="org.poc.aop.annotation.Performing" />

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

public class Audience { .. }
    Bookmark Topic Watch Topic
  • New Topic