In-depth understanding of the static agent with JDK dynamic proxies

Proxy mode

In-depth understanding of the static agent with JDK dynamic proxies

Brief introduction

Proxy mode is a common design pattern has its shadow AOP, RPC and many other frame

  • Existing proxy object value is mainly used to block access to real business objects;
  • Proxy object has the target object (real business objects) to achieve a common interface or inherit from the same class;
  • Proxy object to the target object is enhanced, so that the message preprocessing and postprocessing.

Definition and structure

** Definition: ** provide a proxy to control access to the object to other objects.

In-depth understanding of the static agent with JDK dynamic proxies

Proxy mode mainly includes three characters, i.e. characters abstract theme (the Subject), the role of the delegate (the agent role, Proxied) Role and a proxy (the Proxy), as shown in FIG.

  • Abstract topics roles: can be an interface, it can be abstract;
  • Principal Role: true thematic roles, specific business logic executor;
  • Acting Role: interior includes a reference to the real object RealSubject responsible call to the real subject of the role and doing pre- and post-treatment process before and after the real theme of the role.

Static agent to achieve

In-depth understanding of the static agent with JDK dynamic proxies

Static agent in three steps

1. Define the business interface

public interface HelloService {
 String hello(String name);
 String hi(String msg);
}

2. Implement service interfaces

public class HelloServiceImpl implements HelloService{
 @Override
 public String hello(String name) {
 return "Hello " + name;
 }
 @Override
 public String hi(String msg) {
 return "Hi, " + msg;
 }
}

3. The management class and implement the business interface

public class HelloServiceProxy implements HelloService {
 private HelloService helloService;
 public HelloServiceProxy(HelloService helloService) {
 this.helloService = helloService;
 }
 @Override
 public String hello(String name) {
 System.out.println("预处理...");
 String result = helloService.hello(name);
 System.out.println(result);
 System.out.println("后处理...");
 return result;
 }
 @Override
 public String hi(String msg) {
 System.out.println("预处理...");
 String result = helloService.hi(msg);
 System.out.println(result);
 System.out.println("后处理...");
 return result;
 }
}

Finally, it can be called by the client

public class Main {
 public static void main(String[] args){
 HelloService helloService = new HelloServiceImpl();
 HelloServiceProxy helloServiceProxy = new HelloServiceProxy(helloService);
 helloServiceProxy.hello("Panda");
 helloServiceProxy.hi("Panda");
 }
}

JDK dynamic proxies

In-depth understanding of the static agent with JDK dynamic proxies

Dynamic proxy can be easily related to the method of the delegate class for unified enhancement processing, such as adding a number of method calls, adding logging and so on. Dynamic proxies are divided into JDK dynamic proxies and dynamic proxy cglib two categories, the paper mainly discusses JDK dynamic proxies

JDK dynamic proxy using the steps

  • Creating interfaces and classes proxied
// 抽象主题角色
public interface HelloService {
 String hello(String name);
 String hi(String msg);
}
// 具体(真实)主题角色
public class HelloServiceImpl implements HelloService{
 @Override
 public String hello(String name) {
 return "Hello " + name;
 }
 @Override
 public String hi(String msg) {
 return "Hi, " + msg;
 }
}
  • InvocationHandler implement interfaces
public class MyInvocationHandler implements InvocationHandler{
 // 真实业务对象
 private Object target;
 public MyInvocationHandler(Object target){
 this.target = target;
 }

 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 // 增强逻辑
 System.out.println("PROXY : " + proxy.getClass().getName());
 // 反射调用,目标方法
 Object result = method.invoke(target, args);
 // 增强逻辑
 System.out.println(method.getName() + " : " + result);
 return result;
 }
}
  • Create a proxy class and generate the corresponding proxy object
// 生成代理类的class对象
Class<?> clazz = Proxy.getProxyClass(helloService.getClass().getClassLoader(), helloService
 .getClass().getInterfaces());
// 创建InvocationHandler
InvocationHandler myInvocationHandler = new MyInvocationHandler(helloService);
// 获取代理类的构造器对象
Constructor constructor = clazz.getConstructor(new Class[] {InvocationHandler.class});
// 反射创建代理对象
HelloService proxy = (HelloService)constructor.newInstance(myInvocationHandler);
  • Use a proxy
proxy.hello("rico");
proxy.hi("panda");

Personal Aspect:

  1. Dynamic proxy key technology is reflected;
  2. Proxy object to the target object is enhanced, so that the message preprocessing and postprocessing;
  3. InvocationHandler in invoke () method is embodied complete logical proxy class, enhanced logic and business logic for the real reflection of the cut to be performed includes;
  4. Use JDK dynamic proxy generation for a real business object proxy, only need to specify the target interface, class loader and the specific target interface InvocationHandler can be.
  5. Typical applications JDK dynamic proxies include, but are not limited to AOP, RPC, Struts2, Spring and other important classical framework.

This article ends, like a friend little bit of praise and attention, thanks! !

Guess you like

Origin blog.csdn.net/qwe123147369/article/details/91523711