Javassist进行。什么是主要想法和实际使用的地方? [英] Javassist. What is the main idea and where real use?

查看:125
本文介绍了Javassist进行。什么是主要想法和实际使用的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Javassist是一个Java库,提供了一种操作应用程序的Java字节码的方法。

I know that Javassist is a Java library providing a means to manipulate the Java bytecode of an application.

好的,但为什么我们需要操作字节码?

Ok, but why we need manipulate bytecode?

任何真实的例子?
任何真正的应用程序,使用javassist?

Any real example? Any real app, where javassist used?

推荐答案

一个常见的应用程序是在运行时生成代理类,即在运行时创建一个子类,拦截所有方法调用。示例:

A common application is to generate proxy classes at runtime, i.e. to create a subclass at runtime that intercepts all method invocations. Examples:

Hibernate 使用Proxies拦截实体上的方法调用以实现延迟加载,即在首次访问时从数据库中获取对象。

Hibernate uses Proxies to intercept method invocations on entities to implement lazy loading, i.e. fetching the object from the database when it is first accessed.

Spring Framework 使用Proxies实现其AOP支持,其中包括支持声明性事务。它还使用代理来强制执行适当的范围设计。

The Spring Framework uses Proxies to implement its AOP support, which among other things powers its support for declarative transactions. It also uses proxies to enforce proper scoping.

EJB 使用代理实现容器管理事务,授权检查以及应用用户定义的拦截器。

EJB uses proxies to implement container managed transactions, authorization checking, and to apply user-defined interceptors.

CDI 实现还必须代理托管bean以确保正确的范围。我怀疑他们也使用字节码工程库。

CDI implementations must also proxy the managed beans to ensure proper scoping. I suspect they use a byte code engineering library, too.

最近使用Javassist通过截取来实现方法返回值的透明缓存所有方法调用,只在第一次调用时委托给超级实现。

I recently used Javassist to implement a transparent cache for method return values, by intercepting all method invocations and only delegating to the super implementation on the first invocation.

注意 java.lang.reflect.Proxy 可以在运行时生成代理类,但只能实现接口,而不能扩展类。所有上述用例都需要代理类。

Note that java.lang.reflect.Proxy can generate proxy classes at runtime, but can only implement interfaces, not extend a class. All of the above use cases require the proxying of classes.

这篇关于Javassist进行。什么是主要想法和实际使用的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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