Announcement Announcement Module
Collapse
No announcement yet.
AspectJ Autoproxy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 11:38 AM.

  • #2
    Are your beans in the applicationcontext, both the performer and the aspect have to be in there.

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment

        Working...
        X