Results 1 to 4 of 4

Thread: AspectJ Autoproxy

  1. #1
    Join Date
    Dec 2009
    Posts
    22

    Question AspectJ Autoproxy

    I'm learning spring (via "Spring in Action") and to that end I put together a simple app. While the DI works, the aspect does not. Following is my code, anyone see what is wrong?

    Code:
    package learn.spring.implementations;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class Audience {
    	public Audience() {}
    	
    	@Pointcut("execution(* *.perform(..))")
    	public void performance() {}
    	
    	@Before("performance()")
    	public void takeSeats() {
    		System.out.println("Audience is taking their seats");
    	}
    	
    }
    Code:
    package learn.spring.implementations;
    
    import learn.spring.interfaces.Performer;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class Performance {
    		
    	public static void main(String[] args){
    		ApplicationContext context = new FileSystemXmlApplicationContext("src/learn/spring/config/learn-spring.xml");
    		Performer performer = (Performer) context.getBean("musician");
    		performer.perform();
    	}
    	
    }
    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"
    xmlns:context="http://www.springframework.org/schema/context"
    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
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    <context:annotation-config />
    <aop:aspectj-autoproxy />
    ...
    </beans>
    Last edited by abking; Dec 24th, 2009 at 11:38 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Are your beans in the applicationcontext, both the performer and the aspect have to be in there.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by abking View Post
    I'm learning spring (via "Spring in Action") and to that end I put together a simple app. While the DI works, the aspect does not. Following is my code, anyone see what is wrong?

    ...
    Pointcut 'execution(* *.perform(..))' matches all methods named 'perform()' of classed from default package. I assume that your 'Performer' class belongs to the 'learn.spring.implementations' package (as 'Performance' class does), hence, no pointcut is matched.

  4. #4
    Join Date
    Dec 2009
    Posts
    22

    Default

    Quote Originally Posted by Marten Deinum View Post
    Are your beans in the applicationcontext, both the performer and the aspect have to be in there.
    The issue was an incorrect aspect id in the application context; upon correcting that, it worked. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •