1, AOP overview
AOP: its full name is Aspect Oriented Programming, that is, Aspect Oriented Programming. It extracts the repeated code of the program. When it needs to be executed, it uses the technology of dynamic agent to enhance the existing methods without modifying the source code.
Function: during the running of the program, the existing methods are enhanced without modifying the source code
Advantages: reduce duplicate code, improve development efficiency, convenient maintenance
Implementation: using dynamic agent technology
2, Dynamic agent
Bytecode is created as you use it and loaded as you use it. Static proxy is created and loaded as soon as bytecode comes up. Decorator mode is an embodiment of static agent.
Dynamic agents are commonly used in two ways:
- Interface based dynamic agent
- Provider: the official Proxy class of JDK.
- Requirement: the proxy class implements at least one interface.
- Subclass based dynamic agent
- Provider: CGLIB of a third party. If asmxxxx exception is reported, asm.exe needs to be imported jar.
- Requirement: the proxied class cannot be a class modified with final (final class).
3, Using JDK dynamic proxy
Using Java lang.reflect. The newProxyInstance() method in the proxy class creates a proxy object
Method introduction:
- Newproxyinstance (classloader loader, class <? > [] interfaces, invocationhandler h): returns a proxy class instance of the specified interface, which can assign method calls to the specified call handler.
- Classloader: defines the class loader of the proxy class
- Class<?> [] interfaces: list of interfaces to be implemented by proxy class. Multiple interfaces are supported
- InvocationHandler h: implement the InvocationHandler interface, create proxy objects, and write enhanced methods.
Step 1: create an interface to be enhanced
public interface UserDao { public int add(int a, int b); public String update(String id); }
Step 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 is executed..."); return a + b; } @Override public String update(String id) { System.out.println("update Method is executed..."); return id; } }
Step 3: create the JDK interface implementation class proxy object
The UserDaoProxy class is in step 4
public class JDKProxy { public static void main(String[] args) { // Create a proxy object for the jdk Class[] interfaces = {UserDao.class}; UserDaoImpl userDao = new UserDaoImpl(); UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao)); } }
Step 4: create proxy object
class UserDaoProxy implements InvocationHandler { // Enhanced logic @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }
Step 5: pass in the objects to be enhanced
class UserDaoProxy implements InvocationHandler { // Pass in the proxy object of whoever it is // You can use parameterized construction to pass. Here, the use of Object can be more general. As long as it is the implementation class of UserDao, it can be passed in public UserDaoProxy(Object obj) { this.obj = obj; } private Object obj; // Enhanced logic @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }
Step 6: write the logic to enhance the code
class UserDaoProxy implements InvocationHandler { // Pass in the proxy object of whoever it is // You can use parameterized construction to pass. Here, the use of Object can be more general. As long as it is the implementation class of UserDao, it can be passed in public UserDaoProxy(Object obj) { this.obj = obj; } private Object obj; // Enhanced logic @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Method to perform the previous operation System.out.println("Method to perform the previous operation...." + method.getName() + ": Parameters passed..." + Arrays.toString(args)); // Method execution Object res = method.invoke(obj, args); // Operation after method execution System.out.println("Operation after method execution...." + obj + ",Return value of method:" + res); // Returns the return value of the method return res; } }
Step 7: call the enhancement method
public class JDKProxy { public static void main(String[] args) { // Create a proxy object for the jdk Class[] interfaces = {UserDao.class}; UserDaoImpl userDao = new UserDaoImpl(); UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao)); int res = dao.add(1, 5); System.out.println("When testing, the return value of the method is:" + res); } }
Results can be obtained:
4, AOP related terms
Joinpoint
Refers to the point intercepted. In spring, it refers to methods, because spring only supports connection points of method types.
Simply put, the methods in the class that can be enhanced are called join points.
Pointcut
Refers to the definition of Joinpoint to be intercepted.
Generally speaking, the method that is actually enhanced is called the entry point.
Advice (notification / enhancement)
It refers to what to do after intercepting the Joinpoint.
The logical part of the actual enhancement is called notification (enhancement)
Type of notification:
Pre notification: notification before the enhanced method is executed
Post notification: notification after the enhanced method is executed
Exception notification: notification of exceptions in the enhanced method
Final notification: it will be executed whether there is an exception or not, which is similar to the finally of an exception
Surround Notifications: notifications made before and after the enhanced method is executed
Aspect (aspect)
It is the combination of entry point and notification (Introduction).
Simply put: the process of applying notifications to pointcuts
Introduction
It is a special notification. Some methods or fields can be dynamically added to the class at run time without modifying the class code.
Target (target object)
The target object of the proxy.
Weaving
The process of applying enhancements to the target object to create a new proxy object. spring uses dynamic proxy weaving, while AspectJ uses compile time weaving and class load time weaving.
Proxy (proxy)
After a class is woven and enhanced by AOP, a result proxy class is generated.
The Spring framework monitors the execution of pointcut methods. Once it is monitored that the pointcut method is running, the proxy mechanism is used to dynamically create the proxy object of the target object. According to the notification category, the corresponding function of the notification is woven into the corresponding position of the proxy object to complete the complete code logic operation.