Parameter meaning of Java Cglib dynamic proxy intercept

How to use CGLIB to create a dynamic proxy, there are already a lot of information on the Internet, so I won't go into details here.

Straight to the conclusion.

when we use custom class

private static class MethodInterceptorImpl implements MethodInterceptor {
    private  Object  target;
    public MethodInterceptorImpl(Object  object){
        this.target= object;
    }
    @Override
    public Object intercept(Object obj, Method method, Object[] args,MethodProxy proxyMethod) throws Throwable {
        proxyMethod 和  obj
       1、method.invoke(obj) 

       2、method.invoke(target)

       3、proxyMethod.invoke(obj)

       4、proxyMethod.invokeSuper(obj)

       5、proxyMethod.invoke(target)

       6、proxyMethod.invokeSuper(target)
    }
}

Here we mainly talk about the usage of proxyMethod and obj. There are the following calls:

obj is the proxy object created by cglib, and target is the proxied object of the dynamic proxy, which is usually passed in before we generate

1、method.invoke(obj) 

2、method.invoke(target)

3、proxyMethod.invoke(obj)

4、proxyMethod.invokeSuper(obj)

5、proxyMethod.invoke(target)

6、proxyMethod.invokeSuper(target)

The conclusion is :

2, 4, 5 can be executed successfully.

1, 3 Infinite loop stack overflow, 6 Type conversion error.

Reason :

cglib can be understood as creating a dynamic proxy by inheriting subclasses, then we can understand that target is the parent class, and obj is the subclass generated by the dynamic proxy.

The proxyMethod.invoke method calls the method of the proxied object (parent class), so the input parameter can only be passed in the target; passing in the proxy object obj will cause an infinite loop.

The proxyMethod.invokeSuper method is a method for invoking the proxy object, and the input parameter can only be passed in obj; if the target is passed in, a type error will be reported.

In the same way, the method is originally the method of the parent class, so the input parameter can only be passed to the target. Passing in the proxy object obj will cause an infinite loop.

Guess you like

Origin blog.csdn.net/yytree123/article/details/124693057