带有@Component批注的@Aspect如何在后台运行 [英] How @Aspect with @Component annotation works under the hood

查看:113
本文介绍了带有@Component批注的@Aspect如何在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找答案,但是到目前为止还没有运气,因此我来这里是为了表达一些智慧.

I've been looking for an answer for a while, but no luck so far, thus I'm coming here for some words of wisdom.

我已经使用@Aspect注释创建了一个方面,因为我需要@Autowire一些单例依赖项,所以我决定使用@Component注释此方面类,并让Spring来实现魔术.它有效,但是...

I've created an aspect using @Aspect annotation, because I need to @Autowire some singleton dependencies I've decided to annotate this aspect class with @Component and let the Spring to do the magic. It works, however ...

我对AOP概念,它的编织方式和不同风格(cglib与Aspectj)相当熟悉,但是对我来说,它在幕后如何工作还不是很直观.

I'm fairly familiar with AOP concept, what's weaving and different flavors of it (cglib vs aspectj) but it's not fully intuitive to me how it works under the hood.

@Component表示给定的类在给定的上下文中将是单例,@Aspect表示将在运行时/编译期间将方面类的内容以某种方式编织到目标类中,并且该目标类不是单例,而是例如原型.那我最后要说的是什么?

@Component means a given class will be a singleton within a given context, @Aspect means that the content of an aspect class will be somehow weaved into the target class during runtime/compilation - and this target class is not a singleton but prototype for instance. So what I'm ending up with at the end?

推荐答案

Spring AOP不会编译时间,也不会修改建议目标的代码.相反,它可以与围绕连接点编织的代理一起使用.这就是为什么Spring AOP方面像其他任何Spring Proxy一样被用作(单个)组件,对其字段进行自动装配等的原因.

Spring AOP does not do compile-time-weaving and does not modify the code of the advised target. Instead it works with proxies that are weaved around the joinpoints. That is why Spring AOP aspects and be used as (singleton) components, have their fields autowired, etc., like any other Spring Proxy.

这也是Spring AOP方面仅适用于公共方法执行而不适用于字段访问等原因的原因.

It is also the reason why Spring AOP aspects only work for public method executions, not field accesses and the like.

该文档写得很好,并且尽可能详细地介绍了(或很少)细节: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

The documentation is quite well written and goes into as much (or as little) detail as one might like: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

这篇关于带有@Component批注的@Aspect如何在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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