The two core points of dynamic proxy are: the behavior of the proxy and the agency.

For example, in college, many students are called others bring meal at lunchtime, there was a man H special kind, want to have an idea, he is at the door hung a GongShiPai, who want to look for a person to write a lunch every day on the bulletin board to write down their want to eat meal, H each meal and then write down those who want to eat something directly to help everybody to buy rice. This is the process of a typical agent. Here, the act of agent is to bring food, and the agency is H. Moreover, the agency behavior is decoupled from the agency.

Let’s use the JDK’s proxy mechanism to implement the above example based on this example.

First, we create a proxy behavior class interface, BuyLunchInt (because there may be many people who need to bring meals and different meals for inheritance implementation)

package proxy;
/**
 * @Author darrenqiao
 */
public interface BuyLunchInt {
    void buyLunch();
}
Copy the code

Next, we implement the agent based on the interface of the agent behavior (the implementation of the agent is the core)

We mainly use two classes in the two Reflection packages, the Invocationhandler and Proxy classes.

  • ProxyClass creates a proxy instance from the incoming class information
  • InvocationHandlerImplement the execution of the proxy instance method by implementing the Invoke method
package proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @author Darrenqiao * An agent class that implements an agent-based behavior interface Public class ProxyAgent implements InvocationHandler {class ProxyAgent implements InvocationHandler {class ProxyAgent implements InvocationHandler { private Object target; public Object create(Object target) { this.target = target;returnProxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); } // Implement proxy mechanism, @override public Object invoke(Object proxy, Method Method, Object[] args) throws Throwable { System.out.println("Let's see who needs my help with dinner.");
        method.invoke(target, args);
        System.out.println("Ah, your meal.");
        returnnull; }}Copy the code

Then, who needs to bring the meal, what meal, implement the interface BuyLunchInt and write to the bulletin board configure.properties

package proxy;
/**
 * @Author darrenqiao
 */

public class DarrenBuyLunch implements BuyLunchInt {
    @Override
    public void buyLunch() {
        System.out.println("Darren wants fried rice."); }}Copy the code

class=proxy.DarrenBuyLunch

Finally, in the main method, the first steps are to see if there are any objects on the bulletin board configure.properties that require a proxy. If not, quit.

import proxy.BuyLunchInt;
import proxy.ProxyAgent;

import java.io.*;
import java.util.Properties;

/**
 * @Author darrenqiao
 */
public class Main {

    static Properties prop = new Properties();

    static void initInputStream = new BufferedInputStream(new FileInputStream()"C:\\zongpengq\\code\\testDynamicProxy\\src\\configure.properties"));
            prop.load(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        init();
        if (prop.size() == 0){
            System.out.println("No one's bringing a meal today.");
            return; } // create agent ProxyAgent ProxyAgent = new ProxyAgent();for(String s : prop.stringPropertyNames()) { String className = prop.getProperty(s); Class classInfo = Class.forName(className); // Create a concrete proxy object BuyLunchInt buyLunch = (BuyLunchInt) classinfo.newinstance (); BuyLunchInt proxy = (BuyLunchInt)proxyAgent. Create (BuyLunchInt); // proxy.buylunch (); }}}Copy the code

Let’s look at the results of the run:

If no one needs to bring a meal (i.e., empty the bulletin board configure.properties), the result is as follows

If someone needs to bring a meal, as Darren did, in configure.properties, the result is as follows