简体   繁体   English

与Spring中的AOP配置有关的一些疑问

[英]Some doubts related to the AOP configuration in Spring

I am studying for Spring Core certification and I have a doubt related how Spring handle AOP. 我正在学习Spring Core认证,并且对Spring如何处理AOP有疑问。

Reading the documentation it seems to understand that exist 2 way to obtain AOP in Java: 阅读文档似乎可以理解存在两种获取Java AOP的方法:

  1. Using AspectJ that using byte code modification for aspect weaving offers a full-blown Aspect Oriented Programming language. 使用AspectJ时 ,使用字节码修改进行方面编织可提供一种成熟的面向方面的编程语言。 (so it seems to me that AspectJ is a differnt language that can be integrated with Java to offer it the AOP features). (因此在我看来, AspectJ是一种可以与Java集成以提供AOP功能的不同语言)。

  2. Spring AOP : used in Spring framework that uses dynamic proxies for aspect weaving instead the bytecode modification. Spring AOP :在Spring框架中使用,该框架使用动态代理进行方面编织,而不是字节码修改。

So my doubts are mainly the followings: 所以我的怀疑主要是以下几点:

1) Reading the documentations found the following method to add the AOP support to my Spring application: 1)阅读文档发现以下方法可以将AOP支持添加到我的Spring应用程序:

USING JAVA CONFIGURATION CLASS: 使用JAVA配置类:

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

USING XML CONFIGURATION: 使用XML配置:

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

As you can see in both configurations there is a reference to AspectJ : 正如您在两种配置中看到的那样,都有对AspectJ的引用:

@EnableAspectJAutoProxy

and

<aop:aspectj-autoproxy />

Why? 为什么? If Spring use Spring AOP instead AspectJ why there is a reference to AspectJ when I configure AOP in Spring? 如果Spring使用Spring AOP代替AspectJ,为什么在Spring中配置AOP时会引用AspectJ

2) In the previous examples are show 2 way to configure Spring: by Java configuration class and by XML configuration . 2)在前面的示例中,显示了配置Spring的2种方法:通过Java配置类和通过XML配置 I know that exist a third way to configure a Spring application: by the use of annotations . 我知道存在配置Spring应用程序的第三种方法:通过使用批注 So exist a way to configure AOP using the annotations? 因此, 存在使用批注配置AOP的方法吗?

I think that these Spring AOP settings with references to AspectJ in their names are indeed more irritating than helpful. 我认为这些Spring AOP设置中名称中涉及到AspectJ的内容确实令人烦恼,没有帮助。 I can understand why you are confused. 我能理解你为什么感到困惑。 Spring AOP is really a different concept from AspectJ. Spring AOP实际上是与AspectJ不同的概念。 As you said: dynamic JDK or CGLIB proxies in Spring AOP versus byte code instrumentation during compile or load time in AspectJ. 如您所说:在AspectJ的编译或加载期间,Spring AOP中的动态JDK或CGLIB代理与字节代码检测相对。 Other differences are: 其他区别是:

  • AspectJ compile-time weaving needs a special compiler called Ajc . AspectJ的编译时编织需要一个称为Ajc的特殊编译器。 It is basically an Eclipse Java compiler Ecj enhanced by the aspect weaver which does the instrumentation. 它基本上是由执行检测的方面weaver增强的Eclipse Java编译器Ecj In contrast, Spring AOP creates dynamic proxies during runtime. 相反,Spring AOP在运行时创建动态代理。
  • In AspectJ there are two syntax variants: native and annotation-based. 在AspectJ中,有两种语法变体:本机和基于注释。 The former is more elegant and expressive, a superset of Java and definitely needs Ajc to be compiled. 前者更优雅,更富有表现力,是Java的超集,肯定需要Ajc进行编译。 The latter uses Java annotations and can be compiled with Javac , but needs the aspect weaver contained both in Ajc (compile time) and in the weaving agent aspectjweaver.jar (load time) to "finish" them and make them usable during runtime. 后者使用Java批注,并且可以使用Javac进行编译, 但是需要同时包含在Ajc (编译时)和编织代理Aspectjweaver.jar (加载时)中的Aspect Weaver,以“完成”它们并使它们在运行时可用。 Both variants need the AspectJ runtime contained in both aspectjrt.jar (very small, used for compile-time-woven aspects during runtime) and aspectjweaver.jar (much bigger, used for load-time weaving, contains both the runtime and the weaver). 这两个变体都需要AspectJrt.jar (很小,在运行时用于编译时编织方面)和Aspectjweaver.jar (更大,用于加载时编织,既包含运行时又包含Weaver )中包含的AspectJ运行时。 。
  • AspectJ works for any Java class, it does not need or even know about the Spring framework. AspectJ适用于任何Java类,它不需要甚至不了解Spring框架。 Spring AOP needs the Spring framework as a foundation, you can only instrument Spring Beans/Components with it, not Spring-agnostic POJOs. Spring AOP需要以Spring框架为基础,您只能使用它来检测Spring Beans / Components,而不能使用与Spring无关的POJO。
  • AspectJ is more efficient because it avoids proxies. AspectJ效率更高,因为它避免了代理。 But Spring AOP is an optional part of the Spring framework anyway, so if you use Spring and method execution interception of Spring Beans is all you need, it makes perfect sense to use it. 但是无论如何,Spring AOP仍然是Spring框架的可选部分,因此,如果您使用Spring并且只需要执行Spring Bean的方法执行拦截,那么使用它就很有意义了。
  • Spring AOP uses a subset of the AspectJ pointcut syntax. Spring AOP使用AspectJ切入点语法的子集。 Maybe this is the subtle reason why Spring AOP uses AspectJ references, but I still think it was a bad decision to not differentiate the two concepts from one another more clearly with regard to nomenclature. 也许这是Spring AOP使用AspectJ引用的微妙原因,但是我仍然认为,就术语而言,不更清晰地区分这两个概念是一个错误的决定。 Besides, the common poinctut language subset is defined in a little JAR called aopalliance.jar because long ago a so-called "AOP Alliance" has defined that syntax. 此外,公共poinctut语言子集在一个名为aopalliance.jar的小JAR中定义,因为很久以前,所谓的“ AOP Alliance”已经定义了该语法。 The dominating and by far most powerful AOP language today is AspectJ, though, so in reality AspectJ (maintained by Eclipse) is the leader in that area, IMO. 但是,当今最主要和迄今为止最强大的AOP语言是AspectJ,因此实际上AspectJ(由Eclipse维护)是该领域的领导者IMO。
  • When I say that Spring AOP uses a subset of AspectJ syntax, conversely it means that AspectJ offers a superset. 当我说Spring AOP使用AspectJ语法的子集时,相反地,这意味着AspectJ提供了一个超集。 There are more pointcut types such as call() , set() , get() etc. and you have way more options to intercept joinpoints and apply cross-cutting concerns to your code base via advice or inter-type definition. 还有更多的切入点类型,例如call()set()get()等,您还有更多选择,可以通过建议或内部类型定义来拦截连接点并将跨切面的关注点应用于代码库。

I do not understand your question #2. 我不明白您的问题2。 The configuration class in your example does use annotations, so there is no third way. 您的示例中的配置类确实使用了注释,因此没有第三种方法。 ;-) But there is an old, really outdated AOP approach in Spring called interceptors. ;-)但是在Spring中有一种古老的,确实过时的AOP方法,称为拦截器。 It is a leftover of an early AOP approach and kind of obsolete nowadays, even though it is still usable. 尽管它仍然可以使用,但它是早期AOP方法的遗留物,如今已​​经过时了。

Both Spring AOP and AspectJ can be configured via XML or annotations within Spring. Spring AOP和AspectJ都可以通过XML或Spring中的注释进行配置。 :-) :-)

Not sure if I completely understand your question, I think you are asking something like: "If I'm using Spring AOP, why do I see references to AspectJ?" 不知道我是否完全理解您的问题,我想您是在问:“如果使用Spring AOP,为什么会看到对AspectJ的引用?”

If that's the case, you should know that Spring is not in competition with AspectJ, but rather leverages AspectJ for AOP. 如果是这样,您应该知道Spring不会与AspectJ 竞争 ,而是可以 AspectJ用于AOP。

See Spring documentation: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html 请参阅Spring文档: http : //docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html

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

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