简体   繁体   English

spring aop代理对象

[英]spring aop proxy object

I'm now reading a Spring book written in Korean language and my English is bad. 我现在正在读一本用韩语写的春季书,我的英语很差。 Please understand it. 请理解。

In the book, it says Spring's AOP initiates class using Dynamic proxy if it uses Interface and it uses CGLIB to initiate class in case of not using Interface. 在本书中,它表示如果Spring使用接口,则使用动态代理启动Spring,并且在不使用接口的情况下使用CGLIB启动类。 I don't clearly understand what it means. 我不清楚它是什么意思。 could you help me understand its deep meaning. 你能帮我理解它的深层含义吗?

I don't know this question is silly or not. 我不知道这个问题是不是很愚蠢。 but I'm just curious THX. 但我只是好奇THX。

A proxy is essentially a mediator between a client and an object such that it implements the object's non-final methods. 代理本质上是客户端和对象之间的中介,以便它实现对象的非最终方法。 Proxying an interface is relatively straightforward since an interface is simply a list of methods which need to be implemented, facilitating the interception of method invocations. 代理接口是相对简单的,因为接口只是需要实现的方法列表,便于拦截方法调用。

The Proxy class in Java is a class that implements a list of interfaces which are specified at runtime. Java中的Proxy类是一个实现运行时指定的接口列表的类。 A proxy then has an InvocationHandler associated with it, which delegates method calls made on the proxy to the object being proxied. 然后,代理具有与之关联的InvocationHandler ,它将在代理上进行的方法调用委托给被代理的对象。 It acts as a level of indirection such that methods are not invoked on the object itself but rather on its proxy. 它充当间接层,因此不会在对象本身上调用方法,而是在其代理上调用方法。 The InvocationHandler has but a single method which needs to be implemented: InvocationHandler只有一个需要实现的方法:

public Object invoke(Object proxy, Method method, Object[] args)

Meanwhile, the client invoking the method can't tell the difference between a proxy and its underlying object representation, nor should it care. 同时,调用该方法的客户端无法区分代理与其底层对象表示之间的区别,也不应该关注它。

Proxying a class dynamically, as opposed to an interface, is not quite as simple. 动态地代理类而不是接口,并不是那么简单。 While Java's Proxy is merely a runtime implementation of an interface or set of interfaces, objects do not have to implement an interface. 虽然Java的Proxy仅仅是一个运行时实现的接口或一组接口,对象没有实现一个接口。 Therefore, proxying classes requires bytecode generation, which is where libraries such as cglib come into play. 因此,代理类需要字节码生成,这是cglib等库发挥作用的地方。 cglib provides support for proxying classes because it can dynamically generate bytecode (ie class files), meaning it can extend classes at runtime in a way that Java's Proxy can implement an interface at runtime. cglib为代理类提供支持,因为它可以动态生成字节码(即类文件),这意味着它可以在运行时以Java的Proxy可以在运行时实现接口的方式扩展类。

There are many uses of proxies. 代理有很多用途。 One such use is in lazy loading. 一种这样的用途是延迟加载。 Lazy loading allows objects in an object graph to be loaded only when they are needed. 延迟加载允许仅在需要时加载对象图中的对象。 Rather than loading them all into memory immediately, which could be expensive and resource-intensive, we can load them on-the-fly when we need to access them, such as iterating over a collection of objects. 而不是将它们全部加载到内存中,这可能是昂贵且资源密集的,我们可以在需要访问它们时即时加载它们,例如迭代对象集合。 Instead of loading the entire collection, we simply load a small set at a time. 我们只需一次加载一小组,而不是加载整个集合。 This can be achieved with proxies. 这可以通过代理来实现。 The proxy represents the lazily-loaded object. 代理表示延迟加载的对象。 The object itself, which might be loaded from a database, is not loaded until a method is invoked on its proxy. 在代理上调用方法之前,不会加载可能从数据库加载的对象本身。 The proxy, which intercepts the method invocation, will then load the object into memory and delegate the method invocation to it. 拦截方法调用的代理然后将对象加载到内存中并将方法调用委托给它。

Here is an example of a lazy-loading implementation: 以下是延迟加载实现的示例:

public abstract class LazilyLoadedObject implements InvocationHandler {

    private Object target;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (target == null) {
            target = loadObject();
        }
        return method.invoke(target, args);
    }

    /**
     * Loads the proxied object. This might be an expensive operation
     * or loading lots of objects could consume a lot of memory, so
     * we only load the object when it's needed.
     */
     protected abstract Object loadObject();

}

The above InvocationHandler would be passed to a proxy so that methods invoked on the proxy would be handled by the InvocationHandler . 上面的InvocationHandler将被传递给代理,以便InvocationHandler处理在代理上InvocationHandler The handler checks to see if the object has been loaded. 处理程序检查对象是否已加载。 If it hasn't, it will call loadObject() , which could be some sort of database query that retrieves the object. 如果没有,它将调用loadObject() ,这可能是某种检索对象的数据库查询。

Proxies are very powerful because they allow for the interception of method invocations. 代理非常强大,因为它们允许拦截方法调用。 Such is the case in AOP. AOP就属于这种情况。

Java Dynamic Proxy is a reflective element of the Java language that allows a user to create a proxy of an interface at runtime. Java动态代理是Java语言的反射元素,允许用户在运行时创建接口的代理。 Being a part of the reflection package, it is a part of Java and it ships with the JRE/JDK. 作为反射包的一部分,它是Java的一部分,它随JRE / JDK一起提供。

CGLIB is a code generation library which has the capability to extend Java classes at runtime. CGLIB是一个代码生成库,它具有在运行时扩展Java类的能力。 Because of this, Spring utilizes this functionality to proxy non-interfaces for its AOP library. 因此,Spring利用此功能为其AOP库代理非接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM