简体   繁体   English

Spring AOP使用AspectJ工作还是什么?

[英]Spring AOP use AspectJ to works or what?

I am studying Spring AOP and I have the following doubt. 我正在研究Spring AOP ,我有以下疑问。

From what I know there are 2 ways to implement AOP behavior into a Java application that are: 据我所知,有两种方法可以将AOP行为实现到Java应用程序中:

  1. AspectJ: that is the first original AOP technology that uses byte code modification for aspect weaving. AspectJ:这是第一个使用字节码修改进行方面编织的原始AOP技术。

  2. Spring AOP: Java-based AOP framework with AspectJ integration that uses dynamic proxies for aspect weaving. Spring AOP:基于Java的AOP框架,使用AspectJ集成,使用动态代理进行方面编织。

My doubts are: what exactly means that Spring AOP is a AOP framework with AspectJ integration? 我的疑问是:究竟什么意味着Spring AOP是一个带有AspectJ集成的AOP框架? So it use in turn AspectJ ? 那么它依次使用AspectJ or what? 或者是什么?

The second doubt is related to the Spring configuration of Spring AOP , I know that I can do it in these way: 第二个疑问与Spring AOP的Spring配置有关,我知道我可以这样做:

1) Using Java configuration class: 1)使用Java配置类:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages=“com.example”)
public class AspectConfig {
    ...
}

2) Using XML: 2)使用XML:

<beans>
    <aop:aspectj-autoproxy />
    <context:component-scan base-package=“com.example” />
</beans>

So, in both configuration it seems that Spring AOP use AspectJ because in these configuration I have: @EnableAspectJAutoProxy and 因此,在两种配置中, Spring AOP似乎都使用AspectJ,因为在这些配置中我有: @EnableAspectJAutoProxy

What it exactly means? 它究竟意味着什么?

This might answer your question - it's an excerpt from mvn dependency:tree for a project that uses spring-aop: 这可能会回答你的问题 - 它是mvn dependency:tree的摘录mvn dependency:tree一个使用spring-aop的项目的mvn dependency:tree

[INFO] |  +- org.springframework:spring-aop:jar:3.2.3.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-aspects:jar:3.2.3.RELEASE:compile
[INFO] |  |  +- org.aspectj:aspectjweaver:jar:1.7.2:compile

As you can see, the Spring dependency transitively includes the AspectJ weaver. 如您所见,Spring依赖项可传递地包含AspectJ编织器。

That being said, the documentation states that 话虽这么说, 文件说明了这一点

Spring 2.0 introduces a simpler and more powerful way of writing custom aspects using either a schema-based approach or the @AspectJ annotation style. Spring 2.0引入了一种使用基于模式的方法或@AspectJ注释样式编写自定义方面的更简单,更强大的方法。 Both of these styles offer fully typed advice and use of the AspectJ pointcut language, while still using Spring AOP for weaving. 这两种样式都提供完全类型的建议和AspectJ切入点语言的使用,同时仍然使用Spring AOP进行编织。

Cheers, 干杯,

At the very begining (until 1.2 version), Spring used to use AOP Alliance as the framework to provide AOP support. 在最开始(直到1.2版本),Spring过去常常使用AOP Alliance作为提供AOP支持的框架。 Then, Spring changed and started to use AspectJ for that. 然后,Spring改变并开始使用AspectJ。

The main difference between AspectJ and Spring AOP is that, firstly, Spring AOP provides a subset of features AspectJ provides and also, Spring AOP uses dynamic proxies as the strategy to implement it, while AspectJ enhances the bytecodes of a compiled class, with their specific compiler (you need to run a post-compile process to get your compiles classes ready to go). AspectJ和Spring AOP之间的主要区别在于,首先,Spring AOP提供了AspectJ提供的功能的子集,并且Spring AOP使用动态代理作为实现它的策略,而AspectJ增强了编译类的字节码,具体取决于它们的特定编译器(你需要运行一个后编译过程来准备你的编译类)。

In other words, Spring AOP uses AspectJ engine behind the scenes to provide AOP, what lets you use the power of that robust framework in a simpler way. 换句话说,Spring AOP在幕后使用AspectJ引擎来提供AOP,这使得您可以以更简单的方式使用该强大框架的强大功能。 But beware Spring AOP does not provide all AspectJ features 但要注意Spring AOP不提供所有AspectJ功能

Take a look at this post for more info 看一下这篇文章了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM