CodeNote_1.0.5_Dynamic Agent

The following is a simple dynamic proxy example that demonstrates a common use of dynamic proxies - to add additional logic before and after method execution:

package JavaNote_101;


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

    // 定义一个接口
    interface Calculator {
    
    
        int add(int a, int b);
    }

    // 实现接口的类
    class CalculatorImpl implements Calculator {
    
    
        public int add(int a, int b) {
    
    
            return a + b;
        }
    }

    // 实现InvocationHandler接口的代理类
    class LoggingProxy implements InvocationHandler {
    
    
        private Object target;

        public LoggingProxy(Object target) {
    
    
            this.target = target;
        }

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
    
            // 在方法执行前添加日志
            System.out.println("方法开始执行:" + method.getName());

            // 调用目标对象的方法
            Object result = method.invoke(target, args);

            // 在方法执行后添加日志
            System.out.println("方法执行结束:" + method.getName());

            return result;
        }
    }

public class Java_105_DynamicProxy {
    
    
        public static void main(String[] args) {
    
    
            // 创建目标对象
            Calculator calculator = new CalculatorImpl();

            // 创建动态代理对象
            Calculator proxy = (Calculator) Proxy.newProxyInstance(
                    calculator.getClass().getClassLoader(),
                    calculator.getClass().getInterfaces(),
                    new LoggingProxy(calculator)
            );

            // 使用代理对象调用方法
            int result = proxy.add(2, 3);
            System.out.println("计算结果:" + result);
        }
    }

In the above example, we defined an interface Calculator and a class CalculatorImpl that implements this interface. Then, we created a proxy class LoggingProxy that implements the InvocationHandler interface, which adds log information before and after method execution. Finally, in the DynamicProxyExample class, we use the Proxy.newProxyInstance() method to create a dynamic proxy object and bind the target object and proxy object together. When we use a proxy object to call a method, the logic of the proxy class is actually triggered through the invoke() method of the proxy object.

One of the benefits of dynamic proxying is that it can add additional functionality to the original class without modifying the original class code. In the above example, we added logging before and after method execution through a dynamic proxy without modifying the original CalculatorImpl class. This flexibility makes dynamic proxies very useful in many scenarios, such as logging, performance monitoring, transaction management, etc.

In addition, dynamic agents can also implement aspect-oriented programming (AOP), which provides better code organization and maintainability by separating some common cross-cutting concerns (such as logging, transactions, security, etc.) from business logic.

Hopefully this example gives you a clear understanding of the benefits and usage of dynamic proxies.

Guess you like

Origin blog.csdn.net/h201601060805/article/details/130754768