@TOC

1 Proxy Mode

The proxy pattern: Provides a proxy for an object to control access to that object. That is, the target object is accessed through a proxy object. The advantage of this is that you can augment the implementation of the target object with additional functional operations, that is, extending the functionality of the target object. (3) There are three main types of proxy mode: static proxy **, dynamic proxy ** (JDK proxy, interface proxy), and Cglib proxy **, which can dynamically create objects in memory. Instead of implementing an interface, it is a dynamic proxy. (4) Schematic diagram of proxy modeCopy the code

2 Static Proxy

2.1 Basic introduction to static code pattern

When static proxies are used, interfaces or parent classes need to be defined. The propied-object (the target object) implements the same interface or inherits the same parent class with the propied-object

2.2 Application Examples

The specific requirements

  1. Define an interface :ITeacherDao

  2. TeacherDao Implements the interface ITeacherDao

  3. To use static proxy, you need to implement ITeacherDAO in the proxy object TeacherDAOProxy as well

  4. The target object is called by calling the proxy object’s methods.

  5. Note: The proxy object implements the same interface as the target object, and then calls the target object’s methods by calling the same methods

Thought Analysis Diagram (Class diagram)

Code implementation

interface

Public interface ITeacherDao {void teach(); // Teaching method}Copy the code

Proxied object

Public class TeacherDao implements ITeacherDao {@override public void teach() {system.out.println (" implements ITeacherDao... ); }}Copy the code

Proxy objects

Public Class TeacherDaoProxy implements ITeacherDao{private ITeacherDao Target; Public TeacherDaoProxy(ITeacherDao Target) {this.target = target; } @override public void teach() {// TODO auto-generated method stub system.out.println (" TODO auto-generated method stub system.out.println... "); / / methods target. Called the (); System.out.println(" Submit... ") ); // method}}Copy the code

The test class

Public class Client {public static void main(String[] args) {// TODO auto-generated method stub TeacherDao teacherDao = new TeacherDao(); TeacherDaoProxy TeacherDaoProxy = new TeacherDaoProxy(teacherDao); // Create a proxy object and pass it to the proxy object. Teacherdaoproxy.teach (); teacherDaoProxy.teach(); teacherDaoProxy.teach(); teacherDaoProxy.teach(); }}Copy the code

2.3 Advantages and disadvantages of static proxy

Disadvantages: Because the proxy object needs to implement the same interface as the target object, there are many proxy classes. Once the interface adds methods, both the target object and the proxy object need to be maintainedCopy the code

3 Dynamic Proxy

3.1 Introduction to dynamic proxy Mode

  1. The proxy object does not need to implement the interface, but the target object (the proxied object) must implement the interface, otherwise dynamic proxies cannot be used

  2. Proxy object generation, is the use of JDK API, dynamic in memory to build proxy object

  3. Dynamic proxies are also called JDK proxies, interface proxies

3.2 JDK API for generating proxy objects

Java.lang.reflect.proxy 2. JDK implements the Proxy only by using the **newProxyInstance** method, which accepts three arguments. static Object newProxyInstance(ClassLoader loader, Class<? > [] interfaces, InvocationHandler h) / / 1. This loader: specify the current target use Class loaders, loader for method of fixed / / 2. The Class <? //3. InvocationHandler h: transaction handler methods are triggered when executing a method on the target object, passing in the currently executing method as a parameterCopy the code

3.3 Application Examples of Dynamic Proxy

3.3.1 Application Instance Requirements

Modify the previous static proxy mode to dynamic proxy mode (i.e., JDK proxy mode)

3.3.2 Train of Thought Diagram (Class Diagram)

3.3.3 Code implementation

interface

Public interface ITeacherDao {void teach(); Void sayHello(String name); }Copy the code

Proxied object

Public class TeacherDao implements ITeacherDao {@override public void teach() {system.out.println (" TeacherDao implements ITeacherDao "); } @Override public void sayHello(String name) { System.out.println("hello " + name); }}Copy the code

Proxy objects

public class ProxyFactory { private Object target; public ProxyFactory(Object target) { this.target = target; Public Object getProxyInstance() {return proxy.newProxyInstance (target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method Method, Object[] args) throws Throwable {system.out.println ("JDK proxy start ~~"); Object returnVal = method.invoke(target, args); System.out.println("JDK proxy submission "); return returnVal; }}); }}Copy the code

The test class

Public class Client {public static void main(String[] args) {// TODO auto-generated method Stub // Create the target ITeacherDao target = new TeacherDao(); ITeacherDao proxyInstance = (ITeacherDao) New ProxyFactory(target).getProxyInstance(); // proxyInstance=class com.sun.proxy.$Proxy0 // proxyInstance=class com.sun.proxy proxyInstance.getClass()); // Proxyinstance.teach (); proxyInstance.sayHello(" tom "); }}Copy the code

4 additional agent

4.1 Introduction to the Cglib proxy mode

Both static proxies and JDK proxies require the target object to implement an interface, but sometimes the target object is a single object and does not implement any interface. In this case, the target object subclass can be used to implement the proxy - this is the Cglib proxy A Cglib proxy is also called a ** subclass proxy, which builds a subclass object in memory to extend the functionality of the target object. Some books also attribute Cglib to a dynamic proxy. (3) Cglib is a powerful, high-performance code generation package that extends Java classes and implements Java interfaces at run time. It is widely used by many AOP frameworks, such as Spring AOP, to implement method interception (4) how to choose proxy patterns in AOP programming: A. The underlying Cglib package uses the bytecode processing framework ASM to convert bytecode and generate new classesCopy the code

4.2 Implementation steps of Cglib proxy mode

Jar asm.jar asm-commons.jar asm-tree.jar cglib-2.2.jar () Otherwise the Java. Lang. IllegalArgumentException: (3) the method of the target object if as final/static, then will not be intercepted and additional business methods that do not perform target objects.Copy the code

4.3 Application example of Cglib proxy Mode

4.3.1 Application Instance Requirements

The previous example is implemented using the Cglib proxy patternCopy the code

4.3.2 Train of Thought Diagram (Class Diagram)

4.3.2 Code implementation

Proxied object

Public class TeacherDao {public String teach() {system.out.println (" cglib "); return "hello"; }}Copy the code

Proxy objects

Public class ProxyFactory implements MethodInterceptor {private Object target; Public ProxyFactory(Object Target) {this.target = target; } public Object getProxyInstance() {//1. Create a utility class Enhancer Enhancer = new Enhancer(); SetSuperclass (target.getClass())); //3. Set the callback function enhancer.setCallback(this); Return enhancer. Create (); } // Override the intercept method, @override public Object Intercept (Object arg0, Method Method, Object[] args, MethodProxy arg3) throws Throwable {system.out.println ("Cglib proxy mode ~~ start "); Object returnVal = method.invoke(target, args); System.out.println("Cglib proxy mode ~~ commit "); return returnVal; }}Copy the code

The test class

Public class Client {public static void main(String[] args) {TeacherDao target = new TeacherDao(); TeacherDao proxyInstance = (TeacherDao)new ProxyFactory(target).getProxyInstance(); String res = proxyinstance.teach (); System.out.println("res=" + res); }}Copy the code