JAVA Foundation - Dynamic Proxy for JDK Implementation

Keywords: Java JDK

Dynamic Proxy in JAVA

The difference between dynamic proxy and static proxy classes is that dynamic proxy can dynamically create proxy classes while the program is running, execute proxy class methods, and run extended methods called by proxy classes.

Dynamic Proxy for JDK Implementation

The dynamic proxy of JDK is implemented through the content of the import java.lang.reflect package. The proxy class needs to call the newProxyInstance method under the Proxy class under the package to return an object of type Object, which is the object that implements the proxy class function. By casting, the object that can be converted to the proxy class can be used by the proxy class

    public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)

Above is the definition of the newProxyInstance method, which has three parameters. The first ClassLoader type is a class loader. This parameter is to load the current proxy class, that is, if the proxy class is named userServiceProxy, where the parameter is userServiceProxy.class.getClassLoader(), and the second is an interface representing the entire proxy class that contains the proxy classTolerant interface

The last parameter is the interface of an InvocationHandler, which has only one invoke method inside to implement the ability of the proxy class to expand content to the proxy class, as defined below

public interface InvocationHandler {
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable;
}

Therefore, the InvocationHandler interface we implemented also needs to override this method, which is also the key to our association of proxy and proxy classes. This method needs to return an object of type Object. We associate proxy and proxy classes through reflection, and in this method, we can define extensions on our own.

Code

Interfaces, implementation classes (proxied classes), facet method classes are no different from static proxies, address stamps: Static Proxy

proxy class

package jdkProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * proxy class
 */
public class UserServiceProxy {

    public static UserService createUserService() {
        UserService userService = new UserServiceImpl();
        MyAspcet aspcet = new MyAspcet();
        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(
            UserServiceProxy.class.getClassLoader(), 
            userService.getClass().getInterfaces(), 
            new InvocationHandler(){
            
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    // TODO Auto-generated method stub
                    aspcet.before();
                    Object retObj = method.invoke(userService, args);
                    System.out.println("Intercepted Return Value" + retObj);
                    aspcet.after();
                    return retObj;
                }
            });
        return userServiceProxy;
    }
    
}

Test Class

package jdkProxy;

/**
 * Test Class
 */
public class test {

    public static void main(String[] args) {
        UserService userService = UserServiceProxy.createUserService();
        userService.add();
        userService.delete();
    }

}

Result

before method
service add
 Intercepted return value null
after method
before method
service delete
 Intercepted return value null
after method
50 original articles published, 54 praised, 4963 visited
Private letter follow

Posted by xpherism on Fri, 21 Feb 2020 17:51:28 -0800