————— the next day —————

— — — — — —

public interface IStudentService { void insertStudent(); void deleteStudent(); } public class StudentService implements IStudentService {public void insertStudent(){// Add student} public void implements IStudentService DeleteStudent (){// deleteStudent}}

Public class StudentService implements IStudentService {public void insertStudent(){system.out.println (” implements IStudentService “); // Add student system.out.println (” Add student successfully “); } public void deleteStudent(){system.out.println (” deleteStudent “); System.out.println(” delete student successfully “); }}

public class StudentServiceProxy implements IStudentService { IStudentService studentService; public StudentServiceProxy(IStudentService studentService){ this.studentService = studentService; } @override public void insertStudent() {system.out.println (” ready to add student “); studentService.insertStudent(); System.out.println(” Add student success “); } @override public void deleteStudent() {system.out.println (” ready to deleteStudent “); studentService.deleteStudent(); System.out.println(” delete student successfully “); }}

In the code above, the proxy class and the business class inherit the same interface and override the methods for adding/removing students.

In the overridden method, we can not only call the original method of the business class, but also perform additional processing before and after the call, such as adding logs, transactions, and so on.

This way, on the client side, once we create the proxy class, we can use it as a business class, which is very convenient:

public class Client { public static void main(String[] args) { IStudentService studentServiceProxy = new StudentServiceProxy(new StudentService()); studentServiceProxy.insertStudent(); studentServiceProxy.deleteStudent(); }}

The Java language, for example, provides a very convenient toolkit for creating dynamic proxies. When we generate dynamic proxies, we need to use the InvocationHandler interface and the Proxy class.

The specific implementation process is as follows:

1. Implement the InvocationHandler interface to define what to do before and after calling a method:

public class MyInvocationHandler implements InvocationHandler { private Object object; public MyInvocationHandler(Object object){ this.object = object; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {system.out.println (method.getName() + “before method call “); method.invoke(object, args); System.out.println(method.getName() + “after method call “); return null; }}

2. Dynamically generate Proxy objects using the newProxyInstance method of Proxy class:

public class Client { public static void main(String[] args) { IStudentService studentService = new StudentService(); InvocationHandler studentInvocationHandler = new MyInvocationHandler(studentService); IStudentService studentServiceProxy = (IStudentService) Proxy.newProxyInstance(studentInvocationHandler.getClass().getClassLoader(), studentService.getClass().getInterfaces(), studentInvocationHandler); studentServiceProxy.insertStudent(); studentServiceProxy.deleteStudent(); }}

Like this article friends, welcome to pay attention to the public number programmer xiao Grey, watch more exciting content