Implementation interface of dynamic proxy module in java

Keywords: Java JDK

Usually, the way to implement the interface is to create his implementation class, so that his implementation class can implement the specific method in the interface. This paper will introduce another way to implement the interface: dynamic proxy mode to implement the interface.

The steps of implementing the interface by dynamic proxy are as follows:
1. Create an implementation class of InvocationHandler interface, which implements invoke(... ) Method, which is called indirectly by the proxy class by its own special object:
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
....
}
Introduction of parameters:
proxy: the real object of the agent;
Method: A method of the proxy object to be invoked
args: The parameters required for this method.

2. In the invoke method, the method name is obtained through the method object to see if it is consistent with the method that needs to be implemented. If it is consistent with the method that needs to be implemented, then the implementation of the method can be completed in this if condition.

public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
            if("ss".equals(method.getName())){
                System.out.println("From here on ss Method.");
                System.out.println("...");
                System.out.println("implement ss Method end.");
            }
}

3. The user handed the interface to the proxy class through the Proxy object. The method the user invoked was the static method of the Proxy class, newProxyInstance(... );

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

Description of parameters:
loader: A ClassLoader object that defines which ClassLoader object loads the generated proxy object
Interfaces: An array of Interface objects, which indicates what interfaces I will provide to the objects I need to proxy. If I provide a set of interfaces to it, then the proxy object declares that it implements the interface (polymorphism), so that I can call the methods in this set of interfaces.
h: An InvocationHandler object represents which InvocationHandler object my dynamic proxy object associates with when calling a method.

java code:

Interface class
package com.zhangyike.jdk.implementInterface;

public interface Speak {
    public void ss();

    public void ss1();
}


//Dynamic proxy class
package com.zhangyike.jdk.implementInterface;

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

/*
 * Implementing Interface by Dynamic Agent
 */
public class ImplementInterface implements InvocationHandler  {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        try {  
            if ("ss".equals(method.getName())) {
                System.out.println("From here on ss Method.");
                System.out.println("...");
                System.out.println("implement ss Method end.");
            }else if("ss1".equals(method.getName())){
                System.out.println("From here on ss1 Method.");
                System.out.println("...");
                System.out.println("implement ss1 Method end.");
            }

        } catch (Throwable t) {  
            t.printStackTrace();  
        }  

        System.out.println("After implementation");  
        return null;  
    }
}

Demo Class:
package com.zhangyike.jdk.implementInterface;

import java.lang.reflect.Proxy;

public class Demo {
    public static void main(String[] args) {
        ImplementInterface ii = new ImplementInterface();

        Speak speak = (Speak)Proxy.newProxyInstance(Speak.class.getClassLoader(), 
                new Class[]{Speak.class}, ii);

        speak.ss();

        System.out.println();
        System.out.println();
        System.out.println();

        speak.ss1();
    }
}

//Results of implementation:
//Start executing the ss method from here.
...
//Execute the ss method to end.
//After implementation



//Start executing the ss1 method from here.
...
//The execution of the ss1 method ends.
//After implementation

Posted by Zepo. on Tue, 09 Jul 2019 14:12:41 -0700