Java Agent Review

basic concept

A proxy is a proxy object of an object. The value of the proxy object is mainly (not the only one) to intercept access to real business objects. Users do not need to directly access the proxy object, and can call the proxy object by accessing the proxy object. method.

The proxy mode (Proxy) accesses the target object through the proxy object, which can enhance additional functions on the basis of the target object, such as adding permissions, access control and auditing. The simple class diagram is as follows:

insert image description here

1. Agents in Java

Agents in Java are divided into static agents and dynamic agents.
Static proxy is relatively simple, just write according to the above class diagram and set the proxy class.
Dynamic proxy is built based on the principle of Java reflection, which can build proxy objects when the code is running. It has the following characteristics:

  1. The proxy object does not need to implement the interface
  2. The proxy object can dynamically change the proxy of an object when the code is running, and can dynamically add methods to the proxy object.

2. Static proxy

Static proxy is relatively simple, and it can be realized directly according to the relationship of the above class diagram. The sample code is as follows:

First is IServicethe code, which defines a update()method:

public interface IService {
    public void update();
}

Then there is the proxied object: ServiceImpl, a simple implementation of the update method:

public class ServiceImpl implements IService{
    @Override
    public void update() {
        System.out.println("更新数据");
    }
}

The proxy class ServiceProxy, which implements the IService interface, can do some additional operations in the Update method:

public class ServiceProxy implements IService{
    private IService iService;

    public ServiceProxy(IService service){
        this.iService = service;
    }
    @Override
    public void update() {
        System.out.println("update前校验操作");
        iService.update();
        System.out.println("update后其他操作");
    }
}

transfer:

        /*****静态代理*******/
        IService testService = new ServiceImpl();
        ServiceProxy proxy = new ServiceProxy(testService);
        proxy.update();

3. Dynamic agent

Java dynamic proxies can Proxy.newProxyInstance()be implemented through methods, which have three parameters:

  • ClassLoader loader: Specify the current target object to use the class loader, the method of obtaining the loader is fixed
  • Class<?>[] interfaces: The type of the interface implemented by the target object, using the generic method to confirm the type
  • InvocationHandler h: event processing, when the method of the target object is executed, the method of the event handler will be triggered, and the method of the currently executing target object will be passed in as a parameter

The sample code is as follows:

        IService testService = new ServiceImpl();
        IService proxy1 = (IService) Proxy.newProxyInstance(testService.getClass().getClassLoader(), testService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("调用方法前检查");
                Object obj = method.invoke(testService,args);
                System.out.println("调用方法后操作");
                return obj;
            }
        });
        proxy1.update();

Fourth, the difference between dynamic proxy and static proxy

Static proxy and dynamic proxy mainly have the following differences:

  • Static proxy can only complete the proxy operation manually. If a new method is added to the proxy class, the proxy class needs to be added synchronously, which violates the principle of opening and closing.
  • Dynamic proxy adopts the method of dynamically generating code at runtime, cancels the extension restriction on the proxy class, and follows the principle of opening and closing.
  • If the dynamic proxy wants to expand the enhanced logic of the target class, combined with the strategy mode, it only needs to add a new strategy class to complete it, without modifying the code of the proxy class.

Guess you like

Origin blog.csdn.net/cat_is_so_cute/article/details/124490321