启用S​​pring AOP或AspectJ [英] Enable Spring AOP or AspectJ

查看:96
本文介绍了启用S​​pring AOP或AspectJ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是这个问题:

@Aspect方面的Spring autowired bean为null

我最初的理解是当使用Spring时AOP,使用@Aspect注释的类被创建为弹簧托管bean,因此依赖注入将正常工作。但是,似乎带有@Aspect注释的对象被创建为弹簧容器外的单例,因此我必须像XML一样配置它,以便将其作为弹簧托管bean启用:

My initial understanding was that when using Spring AOP, classes annotated with @Aspect are created as spring managed beans, so dependency injection would work as normal. However it seems that an object with the @Aspect annotation is created as a singleton outside the spring container, hence me having to configure it in XML like so in order to enable it as a spring managed bean:

<bean id="aspect" class="com.mysite.aspect" factory-method="aspectOf" />

现在这让我很困惑。我认为以下配置将使用spring AOP:

This has now completely confused me. I thought the following configuration would use spring AOP:

<context:component-scan base-package="com.mysite.aspectPackage"/>
<aop:aspectj-autoproxy/>

因此,它将使用组件扫描创建方面bean来扫描@Aspect注释,然后autoproxy将创建一个beanPostProcessor,它使用适当的建议代理我的上下文中的所有bean。然后我想要启用aspectJ我需要一个完全不同的XML配置(顺便说一下,我似乎无法在文档中找到一个例子)。正是这种配置使用aspectJ来创建可以在我的容器之外的方面,或者通过操作字节码而不是代理来工作。

So it would scan for @Aspect annotations using component-scan creating aspect beans, and then autoproxy would create a beanPostProcessor which proxies all beans within my context with the appropriate advice. I then thought to enable aspectJ I would need a completely different XML configuration (which incidentally I can't seem to find an example of in the documentation). It would be this configuration that uses aspectJ to create aspects that would be outside of my container or which work by manipulating bytecode rather than proxying.

注意

这不是关于弹簧AOP和J方面之间差异的问题,这里有很好的阐述:

Note
This is not a question on the difference between spring AOP and aspect J, this is well articulated here:

春天AOP与AspectJ

推荐答案

@Component将创建2个实例,一个在Spring容器内,一个在aspectJ容器内。

@Component will create 2 instances, one inside the Spring container, one inside the aspectJ container.

使用@Configurable允许spring在由aspectj容器实例化时管理类的依赖注入等。

use @Configurable to allow spring to manage dependency injection etc. for your class when instantiated by the aspectj container.

@Aspect是一个由spring-aop支持的aspectj样式注释,其中运行时编织用于处理拦截等。

@Aspect is an aspectj style annotation that is supported by spring-aop, where runtime weaving is used to handle interception etc.

编译时编织允许你忽略字节码中存在的切入点的使用,这是通过aspectj编译器完成的(参见 http://mojo.codehaus.org/aspectj-maven-plugin/ for mvn integration)。

Compile-time weaving allows you to disregard the use of as pointcuts will be present in the bytecode, this is done via the aspectj compiler (See http://mojo.codehaus.org/aspectj-maven-plugin/ for mvn integration) .

无论您使用aspectj编译器还是spring-aop都没有区别,除非您使用工厂/可配置,否则它不会以您希望的方式创建您的方面作为托管bean。

Whether you use the aspectj compiler or spring-aop makes no difference, it wont create your aspect as a managed bean in the way you want unless you use factory / configurable.

严格意义上,Aspectj配置是在您的类中出现的切入点定义等。

Aspectj configuration is, strictly, the pointcut definitions etc that will be present inside your class.

这篇关于启用S​​pring AOP或AspectJ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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