[英]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的方法:
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功能的不同语言)。
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:
其他区别是:
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.