guice AOP 是如何实现的? [英] How is guice AOP implemented?

查看:28
本文介绍了guice AOP 是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处的文档中阅读了有关 guice AOP 的内容:- https://github.com/谷歌/guice/wiki/AOP来自文档:-

I read about guice AOP in the documentation here :- https://github.com/google/guice/wiki/AOP From the documentation :-

在幕后,方法拦截是通过在运行时生成字节码来实现的.Guice 动态创建一个子类,通过覆盖方法应用拦截器.如果您使用的平台不支持字节码生成(例如 Android),则应使用不支持 AOP 的 Guice.

Behind the scenes, method interception is implemented by generating bytecode at runtime. Guice dynamically creates a subclass that applies interceptors by overriding methods. If you are on a platform that doesn't support bytecode generation (such as Android), you should use Guice without AOP support.

文档中扩展子类是什么意思.这是方法拦截器的子类吗?使用反射拦截方法是否有效?我问这个问题的原因是因为在文档中,这一行后面跟着:-

What does the documentation means by extending the subclass. Is this the method interceptor subclass? Does it work by using reflection to intercept the method? The reason I ask this question is because in the documentation, this line is followed by this :-

因此,我们对 guice AOP 有以下限制

Due to this, we have the following limitations on the guice AOP

a) AOP 不能应用于私有方法.

a) AOP cannot be applied to a private method.

我不清楚为什么 AOP 不能应用于私有方法.

Unclear to me why AOP cannot be applied to a private method.

推荐答案

我不清楚为什么 AOP 不能应用于私有方法.

Unclear to me why AOP cannot be applied to a private method.

因为私有方法不是由子类继承的,即没有办法拦截私有方法然后委托给它,因为子类甚至不能调用该方法.这是一个普通的 Java 限制,与 AOP 无关.

Because private methods are not inherited by subclasses, i.e. there is no way to intercept a private method and then delegate to it because the subclass cannot even call that method. This is a normal Java limitation and has nothing to do with AOP specifically.

顺便说一句,如果你想要一个成熟的、强大的 AOP 工具,可以使用任何 JVM 语言并且能够拦截私有方法而无需求助于动态代理(在运行时创建的子类,如 Guice 或 Spring AOP),只需使用 AspectJ.

BTW, if you want a full-fledged, powerful AOP tool working with any JVM language and able to intercept private methods without the need to resort to dynamic proxies (subclasses created during runtime like Guice or Spring AOP), just use AspectJ.

文档中扩展子类的含义是什么.

What does the documentation means by extending the subclass.

JRE 知道动态代理的概念 以便能够拦截方法调用并(可选)在拦截器方法中执行其他操作之前/之后委托给原始调用.这仅适用于接口,但 CGLIB 将此概念扩展到非接口类的子类,这就是为什么在基于代理的 AOP 框架中,例如 Spring AOP(可能是 Guice,不是 100% 确定)使用代理来实现 AOP.AspectJ 的工作方式不同,它不使用或不需要任何动态代理或其他类型的子类.

The JRE knows the concept of dynamic proxies in order to be able to intercept method calls and (optionally) delegate to the original call before/after doing something else in an interceptor method. This works for interfaces only, but CGLIB extends this concept to subclasses of non-interface classes, which is why in proxy-based AOP frameworks such as Spring AOP (and maybe Guice, not 100% sure) proxies are used in order to implement AOP. AspectJ works differently, it does not use or need any dynamic proxies or other types of subclasses.

这篇关于guice AOP 是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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