Introduction of dynamic proxies

Operating on dynamic proxies

I, on the dynamic proxy.

    1. Dynamic Agent : only science methods a role: at run time, create a set of dynamic object interface implementation class specified! (At run time, create a set of objects to achieve the specified interface)

2. The dynamic proxy method: Object proxyObject = Proxy.newProxyInstance (ClassLoader classLoader, Class [] interfaces, InvocationHandler h);

 Methods role: to create a dynamic array of interfaces implemented to achieve the class object of all the specified interface!
Parameters:
① ClassLoader: class loader!
* It is used to load the device, the .class file is loaded into memory, the object is formed Class!
②Class [] interfaces: interfaces are designated to be achieved
③InvocationHandler: proxy object of all the methods (the individual does not perform, getClass ()) will call to invoke InvocationHandler's () method.

3. Dynamic Agent ACTION: Final learn AOP (aspect-oriented programming, this will be detailed in a spring inside), it is somewhat similar with the Decorator Pattern, it is also more flexible than the decorator pattern!

4. Details InvocationHandler:

InvocationHandler has an abstract method to invoke

public Object invoke(Object proxy, Method method, Object[] args);

The invoke () method is invoked at what time!
①. When the proxy object is created? Wrong!
②. When calling the proxy object interface methods implemented? Right!

* Object proxy: the current object, that is, the proxy object! Who's calling the method!
* Method method: the current method (objective method) is called
* Object [] args: arguments!

Dynamic parameters on when proxy object call interface has a corresponding manner how to process inovke

 

The test code is as follows:

 1 import java.lang.reflect.InvocationHandler;
 2 import java.lang.reflect.Method;
 3 import java.lang.reflect.Proxy;
 4 
 5 import org.junit.Test;
 6 
 7 public class Demo01 {
 8     
 9     @Test
10     public void fun01(){
11     
12     ClassLoader classLoader =this.getClass().getClassLoader();
13     InvocationHandler invocationHandler =new InvocationHandler() {
14         
15         @Override
16         public Object invoke(Object proxy, Method method, Object[] args)
17                 throws Throwable {
18             System.out.println("动态代理被调用了");
19             return "xxx";
20         }
21     };
22     
23     Object proxyobject = Proxy.newProxyInstance(classLoader, new Class[]{A.class,B.class},invocationHandler );
24     A a = (A) proxyobject;
25     B b = (B) proxyobject;
26     a.a();
27     b.b();
28     proxyobject.toString();
29     proxyobject.getClass();//这个方法不会调用invoke为啥,点进去看看Object,发现getClass()方法前面有native修饰表示这个方法为本地方法
30     //带有native的都是为本地方法
31     //proxyobject.equals(null);
32     Object result=a.aaa("hello", 1000);
33     System.out.println(result);//null xxx
34     }
35 }
36 interface A{
37     public void a();
38     public Object aaa (String str,int i);
39 }
40 interface B{
41     public void b();
42 }
动态代理测试代码

 

根据上面的应用我们来写一个小例子:在一个Waiter类的serve方法的前后加上"你好"、"再见"

Waiter接口代码如下:

package cn.itcast.demo.proxy2;

public interface Waiter {
  public void serve();
}

 

 

Waiter实现类ManWaiter代码如下:

package cn.itcast.demo.proxy2;

public class ManWaiter  implements Waiter{
    @Override
    public void serve() {
    System.out.println("服务中...");
        
    }

}

 

操作动态代理的测试类代码如下:

package cn.itcast.demo.proxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.junit.Test;

/**
 * 我们必须要掌握这个案例的东西
 * @author 希
 *
 */
public class Demo01 {
        @Test
        public void fun01(){
            Waiter waiter =new ManWaiter();
            //为了得到代理对象首先准备三个参数
            ClassLoader classLoader =this.getClass().getClassLoader();//本类的类加载器
            Class[] interfaces = {Waiter.class};//代理的对象
            InvocationHandler invocationHandler =new ManWaiterHandler( waiter);
                
            Waiter  waiterProxy =(Waiter) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
            waiterProxy.serve();//代理对象调用增强后的内容
        }
}
class ManWaiterHandler implements InvocationHandler{
    private Waiter waiter ;
    public ManWaiterHandler(Waiter waiter){//创建一个有参构造让别人提供目标对象
        this.waiter=waiter;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        System.out.println("您好");
        this.waiter.serve();
        System.out.println("再见!");
        return null;
    }
    
}

 

动态代理的主要结构如图所示:

目标对象:被增强的对象
代理对象:需要目标对象,然后在目标对象上添加了增强后的对象!
目标方法:增强的内容

代理对象 = 目标对象 + 增强

5.关于类加载器的简单介绍:

Guess you like

Origin www.cnblogs.com/zwxbky/p/11300578.html