• java的代理 静态与动态代理


    代理

    代理就是,就是将对象的控制权交给第三方,可以用来增强功能,两种方式,静态与动态,所谓的静态就是只能代理一种类对象(如果到父类这一层,可以当作同一类哈),动态的话是可以代理多个类型的对象。

    静态代理实现

    静态代理,实际上是是在代理处通过对象的方式调用函数

    package AOP;
    
    public class ProxyServiceA implements IService {
    
        public ProxyServiceA(IService service) {
            super();
            this.service = service;
        }
        //代理对象
        private IService service;
        
        public void service(String name) throws Exception {
            System.out.println("log start");
            try{
            //通过对象本身调用函数或者叫做方法
                service.service(name);
            }
            catch(Exception e)
            {
                 throw e;
            }
            System.out.println("log end");
        }
        
    	public static void main(String[] args) throws Exception {
    		IService service=new ServiceImplA();
            service =new  ProxyServiceA(service);
            service.service("CYW");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    动态代理

    动态代理的话,就是生成动态代理对象了,已经不使用对象本身了。生成代理对象之后,就是使用

    package AOP;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    public class DynaProxyServiceA implements InvocationHandler {
    
        private Object target;//目标对象
       
        public Object bind(Object object){
            this.target = object;
            //生成动态代理对象
            Object obj=Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this);
            return obj;
        }
        //方法调用实现
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object result = null;
            System.out.println(proxy.getClass().getName());
            System.out.println("method:"+method);
            System.out.println("args:"+args);
            System.out.println("target:"+this.target);
            System.out.println("log start");
            try{
            	//方法调用实现,此处为调用处
                 result = method.invoke(this.target, args);
            }
            catch(Exception e)
            {
                 throw e;
            }
            System.out.println("log end");
            return proxy;
        }
        
    	public static void main(String[] args) throws Exception {
    		IService service = (IService) new DynaProxyServiceA().bind(new ServiceImplA());
    		service.service("CYW");
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    其他文件

    package AOP;
    
    public interface IService {
    	public void service(String name) throws Exception;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package AOP;
    
    public interface ITest {
    	public void test(String name) throws Exception;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package AOP;
    
    public class ServiceImplA implements IService,ITest {
    
    	@Override
    	public void service(String name) throws Exception {
    		System.out.println("ServiceImplA service:"+name);
    	}
    
    	@Override
    	public void test(String name) throws Exception {
    		System.out.println("ServiceImplA test:"+name);
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    TCP/IP五层协议栈(2)
    嵌入式学习笔记(63)指针到底是什么
    mulesoft Module 10 quiz 解析
    【电商营销】了解“客户旅程”,提高客户忠诚度
    OPENCV实战分水岭法二
    python3
    Win2016安装安全狗和DVWA
    国外客户发开发信怎么发?写外贸邮件方法?
    关于redux持久化的配置记录
    iOS 新建本地数据库FMDB
  • 原文地址:https://blog.csdn.net/beidideshu/article/details/133421072