8
votes

I've declared my aspects using the @Aspect annotation, but the advice does not seem to get applied. The aspect works in a few other projects that I have, and the key difference seems to be that the other projects are completely wired using annotations, and this particular project is xml wired. The only bean that is annotation wired is the Aspect. So I'm wondering if spring's aspectj support, when using aspectj-autoproxy is sensitive to order that the beans are defined in the xml.

For example, will beans declared after aspectj-autoproxy in xml be considered for AOP pointcuts?

EDIT:

I moved the <aop:aspectj-autoproxy /> until after all beans are created and still no luck.

Basically my code consists of:

@Component
@Aspect 
public class SomeAspect {
    @Pointcut("@annotation(MyAnnotation)")
    public void isX() {}

    @After("isX()") 
    public void XX() {
        System.out.println("Called aspect");
    }
}

And my controller has something like:

public class XController extends AbstractCommandController {
    @MyAnnotation
    public void handleX(...) {
        // do stuff
    }
    @Override
    protected void handle(...) {
        return handleX(...);
    }
}

And then the spring xml is:

<context:component-scan base-package="package.of.some.aspect" />
<aop:aspectj-autoproxy />

<!-- the rest of the beans below -->
<bean id="someController" class="..." />

My previous projects captured and loaded all beans via the component-scan. That's what's different this time.

EDIT2: The other difference is that the other projects are using @Controller, and @RequestMethod. And in this case I'm using a derived class of AbstractCommmandController. I'm wondering if this applies: http://forum.springsource.org/archive/index.php/t-46637.html

Namely that I can't apply advice to any method except handleRequest().

EDIT3:

My latest try is to override handleRequest() and apply my annotation there. Under the assumption that when spring proxies my controller it will see the annotation and apply the advice, since it's calling through the public, externally called method. This still doesn't work.

1
Post some code please; e.g. the appcontext that defines the aspect, the advice and the pointcut(s).abalogh
@abalogh: I have a feeling if I simplify the code it will work. What is the best way to go about debugging aspectj/spring when this stuff doesn't work as expected?Kevin

1 Answers

4
votes

I see that you are calling the method handleX directly from another method in the same class. This will not respect the annotiation, as the work of processing AOP annotations is done by a JDK proxy that wraps your class and exposes the same interfaces.

It's possible that you can work around this by using CGLIB instead of JDK proxies, but in my experience, the most reliable solution is just not to rely on any AOP annotations for methods called internally.