AspectJ和带有AspectJ-Autoproxy的Spring [英] aspectj and spring with aspectj-autoproxy

查看:133
本文介绍了AspectJ和带有AspectJ-Autoproxy的Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用@Aspect注释声明了我的方面,但是该建议似乎没有得到应用.该方面在我拥有的其他一些项目中有效,主要的区别似乎是其他项目使用注释完全连接,而这个特定项目是xml连接.注释连接的唯一bean是Aspect.因此,我想知道在使用Aspectj-autoproxy时spring的AspectJ支持是否对在xml中定义bean的顺序敏感.

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.

例如,在AOP切入点中,是否考虑在xml中的Aspectj-autoproxy之后声明的bean?

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

我移动了<aop:aspectj-autoproxy />,直到所有bean创建完毕,但仍然没有运气.

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

基本上我的代码包括:

@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(...);
    }
}

然后spring xml是:

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="..." />

我以前的项目通过组件扫描捕获并加载了所有bean.这次是不同的.

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

另一个区别是其他项目正在使用@Controller和@RequestMethod.在这种情况下,我使用的是AbstractCommmandController的派生类.我想知道这是否适用: http://forum.springsource.org/archive/index.php/t-46637.html

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

即,除了handleRequest()之外,我无法将建议应用于任何方法.

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

我最近的尝试是重写handleRequest()并在那里应用我的注释.假设当spring代理我的控制器时,它将看到注释并应用建议,因为它是通过公共的,外部调用的方法进行调用的.这仍然行不通.

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.

推荐答案

我看到您正在直接从同一类中的另一个方法调用方法handleX.这将不遵守注释,因为处理AOP注释的工作是由JDK代理完成的,该JDK代理包装了您的类并公开了相同的接口.

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.

您可以通过使用CGLIB而不是JDK代理来解决此问题,但是以我的经验,最可靠的解决方案是不依赖任何AOP注释来内部调用方法.

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.

这篇关于AspectJ和带有AspectJ-Autoproxy的Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆