guice AOP如何实施? [英] How is guice AOP implemented?

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

问题描述

我在此处的文档中了解了guice AOP:- https://github.com/google/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.

顺便说一句,如果您想要使用任何JVM语言的成熟,强大的AOP工具,并且能够在不求助于动态代理的情况下拦截私有方法(在运行时创建的子类,例如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天全站免登陆