AOP (Aspect Oriented Programming, Aspect-Oriented Programming, and Aspect-Oriented Programming) can be used to separate codes irrelevant to business logic (such as logs and permissions) from projects, reduce the burden on related services and make some general requirements (such as transactions) more widely reused. The implementation of AOP uses the Java proxy mechanism.
The proxy mode is a common Java design mode. It features that the proxy class has the same interface as the delegate class. The proxy class is mainly responsible for preprocessing, filtering, and forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class. A proxy class object is associated with a delegate class object. The proxy class object itself does not actually implement the service, but calls the relevant methods of the delegate class object, to provide specific services. According to the agent creation period, the agent class can be divided into two types:
- Static Proxy: the source code is automatically generated by the programmer or a specific tool, and then compiled. Before running the program, the. Class file of the proxy class already exists;
- Dynamic Proxy: a dynamic proxy is created using the reflection mechanism when the program is running.
Static proxy
The proxy object and the proxy object must both implement the same interface. The following code implements AOP through static proxy, and usermanagerproxy proxy represents the adduser method of usermanagerimpl. However, static proxy generally cannot meet the needs of our project development. If there are many business needs, it must act as a proxy for every business method.
JDK dynamic proxy
The dynamic proxy class compares with the static proxy class. The Byte Code of the dynamic proxy class is dynamically generated by the Java reflection mechanism when the program is running, without the need for programmers to manually write its source code. Dynamic proxy not only simplifies programming, but also improves the scalability of the software system, because the Java reflection mechanism can generate any type of dynamic proxy class. The proxy class and invocationhandler interface in the Java. Lang. Reflect package provide the ability to generate dynamic proxy classes. Java proxy can only be a proxy interface, not a proxy class.
Java. lang. reflect. invocationhandler interface, override invoke (Object proxy, method, object [] ARGs) method, proxy object instance by proxy. newproxyinstance (classloader loader, class interfaces, invocationhandler h) method generation, when the method of the contemporary object is executed, the notification Executor (invocationhandler) executes invoke (Object proxy, method, object []
ARGs) method. method. Invoke (Object OBJ, object... ARGs) method. This method calls the basic method represented by this method object for the specified object with the specified parameter. In this case, the agent actually executes the business logic.
Cglib dynamic proxy
JDK's dynamic proxy mechanism can only implement interface classes, but does not implement JDK's dynamic proxy for classes that do not implement interfaces. cglib implements proxy for classes, the principle is to generate a subclass for the specified target class and overwrite the method to implement enhancement. However, because inheritance is used, the final-modified class cannot be used as a proxy.
What is the difference between JDK dynamic proxy and cglib bytecode generation?
- JDK dynamic proxy can only generate a proxy for classes that implement interfaces, but not for classes;
- Cglib is a class implementation proxy. It mainly generates a subclass for the specified class and overwrites the method. Because it is inheritance, it is best not to declare this class or method as final.