启用 Spring AOP 或 AspectJ [英] Enable Spring AOP or AspectJ

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

问题描述

这是这个问题的后续:

@Aspect 方面的 Spring 自动装配 bean 为空

我最初的理解是,在使用 Spring AOP 时,使用 @Aspect 注释的类被创建为 Spring 管理的 bean,因此依赖注入可以正常工作.然而,似乎带有@Aspect 注释的对象在 spring 容器外创建为单例,因此我必须像这样在 XML 中配置它以便将其启用为 spring 管理的 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 注释,然后自动代理将创建一个 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.

注意
这不是关于 spring AOP 和方面 J 之间区别的问题,这里有很好的阐述:

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

Spring AOP vs 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样式注解,其中runtime weaving用于处理拦截等

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

编译时编织允许您忽略使用,因为切入点将出现在字节码中,这是通过 aspectj 编译器完成的(参见 https://www.mojohaus.org/aspectj-maven-plugin/ 用于 mvn 集成).

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 https://www.mojohaus.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.

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

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