zoukankan      html  css  js  c++  java
  • jdk-动态代理

    1.HelloWorld

    package reflect.proxy;
    
    public interface HelloWorld {
        void print();
    }

    2.HelloWorldImpl

    package reflect.proxy;
    
    public class HelloWorldImpl implements HelloWorld{
    
        @Override
        public void print() {
            // TODO Auto-generated method stub
            System.out.println("hello world!");
        }
        
    }

    3.HelloWorldInvocationHandle

    package reflect.proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    public class HelloWorldInvocationHandle implements InvocationHandler{
        private HelloWorld helloWorld;
        public HelloWorldInvocationHandle(HelloWorld helloWorld) {
            this.helloWorld = helloWorld;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("动态代理-begin");
            Object result = method.invoke(helloWorld, args);
            System.out.println("动态代理-end");
            return result;
        }
    
    }

    4.HelloWorldProxyFactory

    package reflect.proxy;
    
    import java.lang.reflect.Proxy;
    
    public class HelloWorldProxyFactory {
        public static HelloWorld getProxy(HelloWorld helloWorld) {
            HelloWorldInvocationHandle handle = new HelloWorldInvocationHandle(helloWorld);
            Object obj = Proxy.newProxyInstance(helloWorld.getClass().getClassLoader(), new Class[]{HelloWorld.class}, handle);
            return (HelloWorld)obj;
        }
        
        public static void main(String[] args) {
            HelloWorld helloWorld = new HelloWorldImpl();
            HelloWorld proxy = HelloWorldProxyFactory.getProxy(helloWorld);
            System.out.println(proxy.getClass().getName());
            proxy.print();
        }
    }
  • 相关阅读:
    es6 语法 (数值扩展)
    手机日期插件 (转加上自己喜欢的)
    仿微信抢红包(js 转)
    默认时间为今天
    es6 语法 (解构赋值)
    es6 语法 (let 和const)
    es6环境搭建
    express 安装和运行
    git 常用操作,下拉,提交,更新,还原
    排序。
  • 原文地址:https://www.cnblogs.com/jerry19890622/p/3287326.html
Copyright © 2011-2022 走看看