• 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();
        }
    }
  • 相关阅读:
    Nginx的访问控制
    远程登录
    Linux的网络命令
    laravel-collect
    laravel-model
    laravel-Macroable
    laravel-容器
    机器学习-Logisitic回归
    机器学习-多变量线性回归
    算法笔记-分支界限法
  • 原文地址:https://www.cnblogs.com/jerry19890622/p/3287326.html
Copyright © 2020-2023  润新知