aspectj and spring with aspectj-autoproxy

核能气质少年 提交于 2019-12-12 10:44:52

问题


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:


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.



来源:https://stackoverflow.com/questions/6390556/aspectj-and-spring-with-aspectj-autoproxy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!