Java 启用 Spring AOP 或 AspectJ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9666922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable Spring AOP or AspectJ
提问by mogronalol
This is following on from this question:
这是从这个问题开始的:
Spring autowired bean for @Aspect aspect is null
@Aspect 方面的 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:
我最初的理解是,在使用 Spring AOP 时,使用 @Aspect 注释的类被创建为 spring 管理的 bean,因此依赖注入可以正常工作。然而,带有@Aspect 注释的对象似乎是在 spring 容器外作为单例创建的,因此我必须像这样在 XML 中配置它,以便将其启用为 spring 管理的 bean:
<bean id="aspect" class="com.mysite.aspect" factory-method="aspectOf" />
This has now completely confused me. I thought the following configuration would use spring AOP:
这让我完全困惑了。我认为以下配置将使用 spring AOP:
<context:component-scan base-package="com.mysite.aspectPackage"/>
<aop:aspectj-autoproxy/>
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.
因此,它将使用组件扫描创建方面 bean 来扫描 @Aspect 注释,然后自动代理将创建一个 beanPostProcessor,它使用适当的建议代理我上下文中的所有 bean。然后我想启用 aspectJ 我需要一个完全不同的 XML 配置(顺便说一句,我似乎无法在文档中找到一个例子)。正是这种配置使用 aspectJ 来创建在我的容器之外的方面,或者通过操作字节码而不是代理来工作。
Note
This is not a question on the difference between spring AOP and aspect J, this is well articulated here:
注意
这不是关于 spring AOP 和方面 J 之间区别的问题,这里有很好的阐述:
采纳答案by MikePatel
@Component will create 2 instances, one inside the Spring container, one inside the aspectJ container.
@Component 将创建 2 个实例,一个在 Spring 容器内,一个在 aspectJ 容器内。
use @Configurable to allow spring to manage dependency injection etc. for your class when instantiated by the aspectj container.
当由 aspectj 容器实例化时,使用 @Configurable 允许 spring 管理你的类的依赖注入等。
@Aspect is an aspectj style annotation that is supported by spring-aop, where runtime weaving is used to handle interception etc.
@Aspect 是 spring-aop 支持的aspectj 风格注解,运行时编织用于处理拦截等。
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 编译器完成的(有关mvn 集成,请参见https://www.mojohaus.org/aspectj-maven-plugin/)。
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 编译器还是 spring-aop 都没有区别,除非您使用工厂/可配置,否则它不会以您想要的方式将您的方面创建为托管 bean。
Aspectj configuration is, strictly, the pointcut definitions etc that will be present inside your class.
严格来说,Aspectj 配置是将出现在您的类中的切入点定义等。
回答by Bozho
@Aspect
is not a spring annotation, and it is not detected by component-scan. So you have to register it somehow as a spring bean. The aspectOf
solution works. You can also try
@Aspect
不是弹簧注释,也不会被组件扫描检测到。所以你必须以某种方式将它注册为一个 spring bean。该aspectOf
解决方案有效。你也可以试试
@Aspect
@Component
回答by user730413
Use
用
@Aspect
@Configurable
@Aspect
@Configurable
Also add "< context:spring-configured />" in your XML configuration file.
还要在 XML 配置文件中添加“< context:spring-configured />”。