9 - Spring5 learning notes - JDK dynamic proxy, AOP operation terms

Posted by Sorrow on Fri, 28 Jan 2022 04:42:48 +0100

JDK dynamic agent

1. Use JDK to dynamically create Proxy objects by using the methods in the Proxy class

(1) Call the newProxyInstance method

Static object newproxyinstance (classloader, class <? > [] interfaces, invocationhandler h)
Returns an instance of the proxy class of the specified interface that dispatches method calls to the specified call handler

Method has three parameters:

First parameter: class loader

The second parameter: the class where the enhancement method is located. The interface implemented by this class supports multiple interfaces

The third parameter: implement this interface InvocationHandler, create proxy objects, and write enhanced methods.

2. Write JDK dynamic agent code

(1) Create interfaces and define methods.

public interface UserDao {
    public int add(int a, int b);

    public String update(String id);
}

(2) Create interface implementation classes and implement methods

public class UserDaoImpl implements UserDao {
    @Override
    public int add(int a, int b) {
        System.out.println("add Method executed...");
        return a + b;
    }
}

(3) Using Proxy classes

public class JDKProxy {
    public static void main(String[] args) {
//        Create interface implementation class proxy object
        Class[] interfaces = {UserDao.class};
        UserDaoImpl userDao = new UserDaoImpl();
        // The Proxy object dao is obtained through the Proxy class
        UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
        int result = dao.add(1, 2);
        System.out.println("result=" + result);
    }
}

// Create proxy object code
class UserDaoProxy implements InvocationHandler {


    private Object object;

    // 1. Pass in the created proxy object
    // Because we need to use the methods in that implementation class.
    // Using parametric construction transfer
    public UserDaoProxy(Object object) {
        this.object = object;
    }

    // Write enhanced logic, that is, added code
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

//        Method -- added code
        System.out.println("Method" + method.getName() + "Parameters passed.." + Arrays.toString(args));
//        Enhanced method execution -- the add method is called.
        Object res = method.invoke(this.object, args);
//        Method -- added code
        System.out.println("Execute after method..." + this.object);
        return res;
    }
}

Console output results:

Method add Parameters passed..[1, 2]
add Method executed...
Execute after method...com.tt.stu.spring5.UserDaoImpl@4590c9c3
result=3

In the above code, if we do not use Proxy, we are a UserDaoImpl object between new, and then call the add method inside.

But now, we need to do some other things before or after the add method, such as judgment, printing and so on.

We use the original method, that is to modify the source code of the add method in UserDaoImpl in the source code and add new operations there.

If we use the Proxy class, we need to use its newProxyInstance to get the Proxy object. At the same time, we need to implement its method in the last parameter InvocationHandler. In this method, we call the add method, and then we can add new operations before and after the add method.

Finally, we get the proxy object and call the original add method. We will find that the add method has more output, that is, new operations are added without modifying the source code.

AOP terminology

class User {
	add()
	
	update()
	
	select()
	
	delete()
}

1. Connection point

Which methods in the class can be enhanced? These methods are called join points

2. Entry point

The method that is actually really enhanced is called the entry point

For example, only the add method is enhanced, and the other three methods are not enhanced.

3. Notification (enhanced)

(1) The logical part of the actual enhancement is called notification (enhancement)

For example, when logging in, add a permission judgment, and the added part is called notification (enhancement)

(2) There are many types of notifications:

  • Pre notification: add enhanced logic in front of the original method.
  • Post notification: add enhanced logic after the original method.
  • Surround notification: enhance the logic before and after the original method.
  • Exception notification: the enhanced logic part executed when an exception occurs in the original method.
  • Final notification: similar to finally, the enhanced logic part will be executed whether there is an exception or not.

4. Section (is an action)

(1) The process of applying notifications to pointcuts

For example, the process of adding permission judgment to the login method is called aspect

Topics: Java Spring AOP